I am trying to prompt the user to enter his or her first name.
I wrote "hello,first_name", where first_name is the name entered by the user.
Then I modified the code as follows: change the prompt to "enter the name of the person you want to write to" and change the output to "Hello, first_name,"following do you like where you are right now in life (y/n)?";
this Is right before I wrote an if statement, letting the user make the decisions on what code to execute, but the input in my a given "conditions" isn't working because its undefined despite me putting assigning them to 'y' and 'n'.
Long story short I don't understand entirely to how to define the first_name and the input, I'm trying to have the user input those values to be assigned to those variables.
The Error list
#include "stdafx.h"
#include "iostream"
#include"string"
using namespace std;
int main()
{
string first_name;
cout << " Please enter your first name (followed by 'enter'):\n";
cin >> first_name;
cout << "Hello," << first_name << " do you like where you are right now in life (y/n)?:\n";
cin >> first_name;
if (input =='y') {
cout << "what do you like about it over,the people?(y/n)";//?:\n"
}
else if (input == 'n') {
cout << " what do you hate about it,the expenses?(y/n)"//?:\n"
}
else cout << "invalid choice";
cout << " what was your friends name again ?" << endl;
string = friends_name;
cin >> friends_name;
}
return 0;
}
}
In line 12 of your program it looks like you've used the variable first_name instead of input. Replace "first_name" with "input".
Eg:
cin>>input
Also as mentioned in the comments, make sure you declare all the variables that you use
Eg:
string first_name;
string input;
string friends_name;
Edited working code looks like:
#include "stdafx.h"
#include "iostream"
#include"string"
using namespace std;
int main(){
string first_name;
string friends_name;
string input;
cout << " Please enter your first name (followed by 'enter'):\n";
cin >> first_name;
cout << "Hello," << first_name << " do you like where you are right now in life (y/n)?:\n";
cin >> input;
if (input =="y")
cout << "what do you like about it over,the people?(y/n)";//?:\n"
else if (input == "n")
cout << " what do you hate about it,the expenses?(y/n)";//?:\n"
else cout << "invalid choice";
cout << " what was your friends name again ?" << endl;
cin >> friends_name;
return 0;
}
Related
The user is prompted to "enter a middle initial". What happens if they enter a space, full name, or maybe a letter followed by a period '.' ?
How can we modify the program to handle this using cin.ignore?
This is the code I currently have:
I commented out the area I'm having trouble with.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string fname, lname;
char MI;
cout << "Please enter your first name: ";
cin >> fname;
cout << "Please enter your middle initial: ";
cin.ignore(1, '\n');
cin.get(MI);
cout << "Please enter your last name: ";
//cin.ignore('\n')
cin >> lname;
cout << "Your name is " << fname << " " << MI << " " << lname << endl;
return 0;
}
When I have this other cin.ignore in it still doesn't do anything and the last name reads the extra inputs. I've tried adding a number of characters to read and it still doesn't fix the problem. When I run it it just skips the input for last name. I also tried changing the last name input to getline but if still didn't do anything.
You can just use std::getline and std::istringstream:
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string fname, lname;
std::string MI;
std::cout << "Please enter your first name: ";
std::getline(std::cin, fname);
std::istringstream iss(fname);
iss >> fname;
do
{
std::cout << "Please enter your middle initial: ";
std::getline(std::cin, MI);
} while (MI.size() != 1);
std::cout << "Please enter your last name: ";
std::cin >> lname;
std::cout << "Your name is " << fname << " " << MI << " " << lname << std::endl;
return 0;
}
Here for fname I have used std::getline to get user input and then I've used std::istringstream to get only one word of the input.
For MI I have made it a string and until and unless the user doesn't provide a single character, the program doesn't continue.
And the lname part is the same.
You should change:
cin.ignore(1, '\n');
cin.get(MI);
To simply:
cin >> MI;
Let operator>> ignore any white space, including line breaks, between the first name and the middle initial.
After reading MI, you can then use the following to ignore everything up to the next input:
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Try this:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
int main ()
{
string fname, lname;
char MI;
cout << "Please enter your first name: ";
cin >> fname;
cout << "Please enter your middle initial: ";
cin >> MI;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Please enter your last name: ";
cin >> lname;
cout << "Your name is " << fname << " " << MI << " " << lname << endl;
return 0;
}
Why does cin keep prompting even if I hit enter?
#include <iostream>
using namespace std;
int main(){
string name = "";
cout << "What's your name?";
cin >> name;
cout << "Hello "";
if (name == "")
cout << "World!";
else
cout << name + "!";
return 0;
}
I want cin >> to stop when I hit enter without typing any words so if the user enters nothing it will show a default Hollow World message and otherwise customized message.
cin >> name;keeps reading until you've entered something. You want a function that just reads a single line. That function is called getline.
#include <iostream>
#include <string>
using namespace std;
int main(){
string name = "";
cout << "What's your name?";
getline(cin, name);
cout << "Hello "";
if (name == ""){
cout << "World!";
else
cout << name + "!";
return 0;
}
Hi i dont know how to allow the user to choose one of the choices i provided in my code and if they picked a choice that was not given then i want the program to return to the last point.
This is my code
#include <iostream>
#include <string>
using namespace std;
int main()
{
//Welcoming message with date//
cout << "Welcome to FILM Screen Cinema" << endl;
cout << endl;
cout << "ENTER STAFF/OPERATOR'S NAME: ";
string name;
cin >> name;
cout << endl;
cout << "ENTER DATE:";
string date;
cin >> date;
cout << endl;
cout << "CHOOSE A MOVIE THAT IS SCREENING TODAY:" << endl;
cout << "[1] Deadpool" << endl << "[2] Goosebumps" <<endl;
string input;
cin >> input;
}
Where it says choose a movie, i want the user to type either 1 or 2 for the choices but if they type any other number then it should say invalid and allow them to type 1 or 2 again. Thank you in advance.
I suspect you want each of the std::cin >> XXX calls to actually query the user for input. That is currently not the case, for example when the user types "John Smith" as the name of the operator, name will be set to "John", and the following std::cin >> date reuses the remaining part "Smith" instead of asking for input again.
To fix this, you can use std::getline.
The first part of your code should thus be replaced by
std::cout << '\n';
std::cout << "ENTER STAFF/OPERATOR'S NAME: ";
std::string name;
std::getline(std::cin, name);
std::cout << '\n';
std::cout << "ENTER DATE:";
std::string date;
std::getline(std::cin, date);
std::cout << '\n';
The same is true for the part where you validate the input. Now you only need to add a while-loop to keep asking the user for new input if the previous one was invalid.
std::cout << "CHOOSE A MOVIE THAT IS SCREENING TODAY:\n";
std::cout << "[1] Deadpool\n"
<< "[2] Goosebumps\n";
std::string input;
std::getline(std::cin, input);
while(input != "1" && input != "2") {
std::cout << "Invalid!\n\n";
std::cout << "CHOOSE A MOVIE THAT IS SCREENING TODAY:\n";
std::cout << "[1] Deadpool\n"
<< "[2] Goosebumps\n";
std::getline(std::cin, input);
}
if (input == "1") {
// do something
} else if (input == "2") {
// do something else
}
Btw, please consider reading “\n” or '\n' or std::endl to std::cout? and Why is “using namespace std” considered bad practice?
I am trying to make it so if userselection = 1, then the user is asked questions to create their contact for an address book. It saves all of the contact info to a struct and then saves to a .txt file. I am very new to C++. This is what I have so far... I keep getting [Error] expected primary-expression before '.' token. <---- HOW CAN I FIX THIS
Also, can anyone offer guidance for how to save the struct to a file?
Thanks.
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
struct person{
string Name;
string Address;
string PhoneNumber;
string Email;
};
int main(){
int userselection = 0;
cout << "What do you want to do? Press 1 to Add Contact -- Press 2 to Search for Contact"<<endl;
cin >> userselection;
if(userselection == '1');
person newPerson;
cout << "What is your Name?" << endl;
cin >> person.Name;
cout << "What is your Address?" << endl;
cin >> person.Address;
cout << "What is your Phone Number?" << endl;
cin >> person.PhoneNumber;
cout << "What is your Email?" << endl;
cin >> person.Email;
}
For the error you describe, you need to access members in the class instance, not the class definition ..
newPerson.Name
rather than
person.Name
Your mistakes were simply associated with syntax. Please read your compiler's error messages in the future.
#include <iostream>
#include <string> // added
using namespace std;
struct person {
string Name;
string Address;
string PhoneNumber;
string Email;
};
int main() {
int userselection = 0;
cout << "What do you want to do? Press 1 to Add Contact -- Press 2 to Search for Contact"<<endl;
cin >> userselection;
if(userselection == 1) { // userselection is int so why compare it to char
person newPerson;
cout << "What is your Name?" << endl;
cin >> newPerson.Name; // assign to object's member not a static member
cout << "What is your Address?" << endl;
cin >> newPerson.Address;
cout << "What is your Phone Number?" << endl;
cin >> newPerson.PhoneNumber;
cout << "What is your Email?" << endl;
cin >> newPerson.Email;
}
}
Why does my code not run properly? As soon as it gets to the if else statement it takes one input from the user and then exits before I can enter anything else. I am not sure if it is due to the function not returning properly but I would really appreciate some help. Thanks.
#include <iostream>
#include <fstream>
using namespace std;
void studentenrollment (char answer);
int main()
{
char answer; //declaring variables for main
cout << "Welcome to Luton Sixth Form" << endl; //greeting the user
cout << "Please State if you are enrolled or not at the sixth form: Y/N" << endl;//giving user options
cin >> answer;//taking options from user
studentenrollment(answer); //calling student enrollment function
return 0;
}
void studentenrollment (char answer)
{
unsigned char name;
int dob;
if (answer=='Y'||answer=='y')
{
cout << "Welcome to higher education" << endl;
cout << "Please state your name" << endl;
cin >> name;
ofstream myfile;
myfile.open("StudentAccess.txt");
myfile << name << endl;
myfile.close();
cout << "Your name is now saved, you have access to the gateway" << endl;
}
else if(answer=='N'||answer=='n')
{
cout << "Please state your name" << endl;
cin >> name;
cout << "Please enter your date of birth" << endl;
cin >> dob;
ofstream myfile;
myfile.open("StudentEnrollment.txt");
myfile << name << dob << endl;
myfile.close();
cout << "You will now go through enrollment" << endl;
}
// return 0;
}
unsigned char name; looks incorrect. Choose char name[MAX_LENGTH]; or std::string name;
What happens
cin >> name; // Read just first character
cin >> dob; // Try to read number, where rest of the name is left in the stream buffer
This certainly looks wrong unless the name is 1 letter wide.
Problem can be this:
cin >> name;
you are entering name but storing it in name - which is just unsigned char. Use a larger array to store the name.
If the variable name can contain more than 1 character you cannot declare it unsigned char, you can declare it std::string. remember the
#include<string>