I have a problem, We are not allowed to use ARRAYS:
Heres a sample output:
How many iterations do you want?: 5
Enter a letter: a
Enter a letter: z
Enter a letter: z
Enter a letter: A
Enter a letter:b
You keyed in lowercase 'a': 1 time(s)
You keyed in lowercase 'b' : 1 time(s)
You keyed in lowercase 'z' : 2 time(s)
You keyed in uppercase 'A' : 1 time(s)
Make a program that asks a user how many letters to be entered, then accepts any letter upper and lower cases then displays how many times the certain letter is typed.
That's still not a question, but does seem to be a request for someone to write it for you, using those guidelines. Sorry, but we only help with programs you're writing, pointing you in the right direction to get your creation working. We just help you learn.
main()
{
int limit,a=0,b=0,A=0,B=0;
char letter;
cout<<"How many iterations do you want?" ;
cin>>limit;
for (int init =1; limit>=init;init++)
{
cout<<"Enter a letter :" ;
cin>>letter;
if (letter=='a')
{
a++;
}
else if (letter=='b')
{
b++;
}
else if (letter=='A')
{
A++;
}
else if (letter=='B')
{
B++;
}
}
if (a>0)
{
cout<<"You keyed in lowercase 'a' :"<<a<<"Time(s)"<<endl;
}
if (b>0)
{
cout<<"You keyed in lowercase 'b' :"<<b<<"Time(s)"<<endl;
}
if (A>0)
{
cout<<"You keyed in uppercase 'A' :"<<A<<"Time(s)"<<endl;
}
if (B>0)
{
cout<<"You keyed in uppercase 'B' :"<<B<<"Time(s)"<<endl;
}
return 0;
}
This is the only idea i have but my instructor says there is a shorter code for this but i am not allowed to use array. I just wanna know the process.-
Creating a dynamic array. When your code read a new character and save it then count++ or a saved character just count++. You should write a function to check its duplicate or not.
Here is my version for your program. It doesn't use arrays or vectors. Please don't try turning this in as your works, but do study it, and figure out how, and why, it works. If you have questions about the program, please ask me. I'll do my best to explain..
#include<iostream>
#include <string>
usingnamespace std;
int main()
{
int limit, character;
string letter="";
char next;
cout << "How many iterations do you want?";
cin >> limit;
for (int init = 0; init < limit; init++)
{
cout << "Enter a letter : ";
cin >> next;
letter += next;
}
for (int alphabet = 65; alphabet < 91; alphabet++) // Capital letters in alphabet
{
character = 0; // Reset count for next letter
for (int init = 0; init < limit; init++) // Run through each character in string input
{
if (letter[init] == char(alphabet)) // Compare the two
character++; // Increase if a match
}
if (character > 0)
cout << "You keyed in uppercase '" << char(alphabet) << "' : " << character << " Time(s)." << endl;
}
for (int alphabet = 97; alphabet < 123; alphabet++) // Lowercase letters in alphabet
{
character = 0;
for (int init = 0; init < limit; init++)
{
if (letter[init] == char(alphabet))
character++;
}
if (character > 0)
cout << "You keyed in lowercase '" << char(alphabet) << "' : " << character << " Time(s)." << endl;
}
cin >> next; // Prevent screen from closing when program finishes
return 0;
}
string letter=""; is the string variable that's going to hold all your char inputs. After being asked how many iterations, or inputs, your going to do, a for loop is created with as many as you entered. Each character you input, is added to the end of the string called letter. They're added in with the letter+=next, which just means, letter equals letter plus the variable called next When finished, you'll have one string with all the inputted letters. In case you're wondering about, for (int alphabet = 65; alphabet < 91; alphabet++). char(65) equals 'A', char(66) is 'B', and so on. So, we're checking if char(65) is equal to each of the inputted letters in the string letter. If yes, the variable, character, is increased by one. At the end of searching your string length, the variable character amount is printed, character is set back to 0, and the complete string is now searched for 'B'. etc.