Question: Write a program that asks a user to input their name one letter at a time, typing | after their last letter to stop entry.
Write a program that asks a user to input their name one letter at
a time, typing | after their last letter to stop entry. Store each letter
into a character array, and then display their name as they would
normally read it. Assume that no name will exceed 256 characters.
This is the code I have so far. The problem I'm encountering is that after the user enters '|' to break to loop and print their name on the screen, it's printing the '|' at the end of their name. Is there a way to make it print only their name, and not the '|' they entered to break the loop? Thanks in advance!
#include
using namespace std;
void intro(); //Protyping "intro" function
int main(int nNumberofArgs, char* pszArgs[]) { char charName[256]; //Declaring array type, name, and size
intro(); //Calling the intro function
for(int i = 0; i < 256; i++) //Beginning the for loop { cin >> charName[i]; //Getting input from user
if (charName[i] == '|') //Assigning how to end the loop { break; //Breaking the loop }
}
cout << "Hello " << charName << "!" << endl; //Outputting user's name on the screen
//wait until user is ready before terminating program //to allow the user to see the program results cout << "Press Enter to continue..." << endl; cin.ignore(10, ' '); cin.get(); return 0; }
void intro() { cout << "Please enter your name one letter at a time, pressing enter " << endl; //Explains the program to the user cout << "after each letter. When you're done entering your name, type |. " << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
