How would I create a while loop so that the program reads in character values from the keyboard until an asterix is entered

  • Thread starter Thread starter StudiousStudent
  • Start date Start date
S

StudiousStudent

Guest
I've recently started to learn c++. One of the tasks I came across asks for me to

• Set up a while loop so that program reads in character values from the keyboard until an asterix is entered.
• You will need to count the number of characters entered. Do not include the asterix in your count.
• Output the number of characters entered.

I think my issue mainly falls in the category of not knowing how to instruct the programme to differentiate between the asterisk (*) and other characters in the same string of word/letters.

int number;
int total;
string input;

bool correct = 0;
do
{

cout << "this loop will stop when an asterisk has been inputted" << endl;
cin >> input;

if (input == "*")
{
number = input.length();
total = number - 1;
cout << "You have " << total << " in your sentence." << endl;
correct = 0;
}
else
{
number = input.length();
total = number;
cout << "You have " << total << " in your sentence." << endl;
correct = 1;
}

} while (correct);

Studious Student

Continue reading...
 
Back
Top