I have been trying to test build this old 'text adventure' thing I found,
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
/////////////////////////////////////////////////////// VARIABLES //////////////////////////////////////////////////////////
string name;
string shipName;
int health;
int crewCount;
int armyTotal, activeArmy;
int casualtiesCount, woundedCount, healedCount;
// 'food' as in a whole meal (beverage, chewables, etc)
int foodCount;
////////////////////////////////////////////////////// INTRODUCTIONS ///////////////////////////////////////////////////////
cout << "What is thy name?\nName: ";
cin >> name;
cout << endl << "What will you name your ship?\nShip Name: ";
cin >> shipName;
cout << "\nSETTING: You are floating through space on giant space cruiser " << endl << "known as the " << shipName << ".\n You are on a random patrol sorti, just looking out for any trouble...";
cout << "Press ENTER to continue...";
cin.get();
cout << "\nFrom here on out, type corresponding number to which choice you want to make.\nPress ENTER to continue...";
cin.get();
//////////////////////////////////////////////////////// BEGINNING ////////////////////////////////////////////////////////
cout << endl << "Admiral " << name << ", we need you on flight deck.";
cout << "1: Go to flight deck.";
cout << "2: Go to kitchen.";
cout << "3: Go to MedBay.";
cout << "4: Do nothing.";
}
and I get an error for:
cin >> name;
where ">>" matches no operands.
I clearly remember this code working at some point I do believe. If I try and skip ahead, I get an error where it can't find an exe (and there is no option to Build Final)
Sorry for not being clear, but I haven't used C++ for a few years now, quite rusty on just about everything. Any sort of wisdom to shed?
You need to #include <string>. That's where the actual operator is defined. Chances are that in the past, <iostream> may have included it perchance, which it's allowed to but not required (or guaranteed) to.
Related
Hoping to free myself from Eclipse and not wanting to keep using the online cpp.sh, I wrote a small program in Cygwin in nano and tried to run it. The code is included for clarity.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
string mystr;
cout << "Enter number: ";
cin >> i;
cout << "You entered: " << i;
cout << " and its double: << i*2 << ".\n";
cin.ignore();
cout << "Name: ";
getline(cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "Team? ";
getline(cin, mystr);
cout << "Go " << mystr << "! \n";
return 0;
}
Trying to run it returns a series of errors, as seen in the picture. Right now, I'm trying to understand why "using" is not recognized. Checking Google found many similar complaints, but never about the command "using," probably because "using" is a common enough word to be using in a different context.
You can't run source code directly. You must compile it first.
Well, a lot of g++ compile errors are because you have
cout << " and its double: << i*2 << ".\n";
when you probably meant
cout << " and its double: " << i*2 << ".\n";
with one more quotation mark
As soon as you insert the " into line 14 it will compile and run, but the last portion will not complete.
I would also create another string near mystr, and use it for getline(). Reusing mystr faults when I compile it.
int main()
{
int i;
string mystr;
string team;
`getline(cin, team);
cout << "Go " << team << "! \n";`
After changing and compiling, I get:
Enter number: 3
You entered: 3 and its double: 6.
Name: Aaron
Hello Aaron.
Team? Meow
Go Meow!
I'm having some minor trouble with this basic C++ quiz program. In the main function, I have the user enter his/her name and I pass this string to the next function, take_quiz. However, I've noticed that if I include a name with a space in it (like a first and last name), an error occurs. For some reason, the amount of letters in the second word produces the same amount of displays of, "Please enter a valid answer (a, b, c, d)." I thought this was strange because that prompt can only occur when the inline function valCheck is used which is after the first cin of a variable in take_quiz. I need some help identifying the issue and correcting it. Thanks!
inline char valCheck(char& input)
{
tolower(input);
while(input < 97 || input > 100)
{
cout << "Please enter a valid answer (a, b, c, d):" << endl;
cin >> input;
}
}
int main(int argc, char *argv[])
{
string name;
cout << "This program will quiz your knowledge of C++. Please enter your name:" << endl;
cin >> name;
cout << "Hello " << name << "! IT'S QUIZ TIME!!!" << endl;
take_quiz(name);
system("PAUSE");
return EXIT_SUCCESS;
}
void take_quiz(string name2)
{
char quiz_results[10];
system("PAUSE");
cout << "\nThe quiz will now begin.\nThis quiz covers topics such as data types, arrays, pointers, etc." << endl
<< "To answer the multiple choice questions,\nsimply input a, b, c, or d according to the given options." << endl
<< "The test will continue regardless if you enter a question wrong or right." << endl
<< "Good luck " << name2 << "!" << endl;
system("PAUSE");
cout << "\n1. What preprocessor command must one include to use the cout and cin function?" << endl
<< "\na. #include <iomanip>" << endl
<< "b. #include <iostream>" << endl
<< "c. #include <cmath>" << endl
<< "d. using namespace std;" << endl;
cin >> quiz_results[0];
valCheck(quiz_results[0]);
Your valCheck() doesn't return anything - according to signature it needs to return a char value. You want to use std::getline(std::cin, str); instead of std::cin if your string contains newlines. std::cin will by default skip whitespaces. Also you're invoking take_quiz() without a prototype function before, so you need to move it above main() or specify the function signature at least above.
The complete program should look like this (you just need to add a check if quiz_results[0] is equal to 'b').
#include <iostream>
#include <string>
using namespace std;
inline char valCheck(char& input){
tolower(input);
while (input < 97 || input > 100){
cout << "Please enter a valid answer (a, b, c, d):" << endl;
cin >> input;
}
return input;
}
void take_quiz(string name2)
{
char quiz_results[10];
system("PAUSE");
cout << "\nThe quiz will now begin.\nThis quiz covers topics such as data types, arrays, pointers, etc." << endl
<< "To answer the multiple choice questions,\nsimply input a, b, c, or d according to the given options." << endl
<< "The test will continue regardless if you enter a question wrong or right." << endl
<< "Good luck " << name2 << "!" << endl;
system("PAUSE");
cout << "\n1. What preprocessor command must one include to use the cout and cin function?" << endl
<< "\na. #include <iomanip>" << endl
<< "b. #include <iostream>" << endl
<< "c. #include <cmath>" << endl
<< "d. using namespace std;" << endl;
cin >> quiz_results[0];
valCheck(quiz_results[0]);
}
int main(int argc, char *argv[]){
string name;
cout << "This program will quiz your knowledge of C++. Please enter your name:" << endl;
std::getline(std::cin, name);
cout << "Hello " << name << "! IT'S QUIZ TIME!!!" << endl;
take_quiz(name);
system("PAUSE");
return EXIT_SUCCESS;
}
I changed how to get string input from User
int main(int argc, char *argv[])
{
char name[100];
cout << "This program will quiz your knowledge of C++. Please enter your name:" << endl;
cin.getline(name,sizeof(name));
//cin >> name;
cout << "Hello " << name << "! IT'S QUIZ TIME!!!" << endl;
take_quiz(name);
system("PAUSE");
return EXIT_SUCCESS;
}
I need a line to separate the users inputs from the results. This is for a basic computer science course.
cout << "Please enter (variable): ";
cin >> (variable);
cout << "\n\n:";
cout << "(results)" << endl;
Is there any other way to get the blank line after the cin, or is this viable code?
You can use endl rather than the \n if you want, but what you have is fine.
What you have is ok. However, this is another, more compact and legible option:
out << "Please enter (variable): ";
cin >> (variable);
cout << endl << "(results)" << endl;
Although the question is old and answered, but the given answers have some pitfalls according to the official GNU documentation.
endl is mistakenly used for newline by many coders, which is not what it is made for, and using it for this purpose renders it less efficient for it's true use case. It is used for flushing the buffers, using it very often for just newlines would make you prone to use it while you are writing text/data in a file and that use would be effecting it's buffering efficiency.
Morever flushing buffer each time you need a newline is not a desireable thing.
So how can you add new-line whenever you use cin
If variable is string
#include <iostream>
#include <string>
using namespace std;
int main()
{
string var;
cout << "Please enter (variable):\n";
getline(cin, var);
cout << "(results)" << var << "\n";
return 0;
}
If variable is not string
cout << "Please enter (variable):\n";
cin >> (variable);
cout << "\n(results)\n";
#include <iostream>
using namespace std;
int main ()
{
int a, b;
cout << "Give 'a' a value: ";
cin >> a;
cout << "" << endl;
cout << "Give 'b' a value: ";
cin >> b;
cout << "" << endl;
return 0;
}
I have the following code. Everything works fine but quite different. I expect the first three cout << come one after the another, so when the first message is shown in the console, the user enters the value, and then the next cout << shows another message and the user enters the name of the book, and then the third cout << shows the last message and the user enters the year. But it shows the first message for, I enter the value, and then it shows the next two messages together. Why?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
string AuthorName;
string AuthorBook;
string YearPublished;
cout << "Please Enter the Author Name" << endl;
cin >> AuthorName;
cout << "Please enter the Author Book" << endl;
cin >> AuthorBook;
cout << "Please enter the year when the book was published" << endl;
cin >> YearPublished;
cout << setw(15) << "Author Name";
cout << setw(15) << "Prominent Work";
cout << setw(15) << "Year Published";
cout << endl << endl;
cout << setw(15) << AuthorName;
cout << setw(15) << AuthorBook;
cout << setw(15) << YearPublished;
cout << endl << endl;
return 0;
}
You need to use getline() since C++ stops reading your input at the first space in strings using cin.
I need to take in the make model and years of the car the particular product fits. The program is going to write a file with text in it that will be an outline of the html. I need with the crucial information inserted into the correct spots to quickly be able to add products to our web page. When I use a switch statement or if statements to separate the categories of the products, the input gets messed up and does not let me answer one of the questions. I can't just use cin because I need to take dates that look like "2008 - 2009 - 2010 - 2011".
Here is what I've got so far! This one is just regular cin, I have tried getline you could quickly c I started yesterday and I'm pretty new so forgive me if this is an easy fix, but I cant find anything.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void proinfo();
int main()
{
//Sytem Information
system("TITLE This is a Beta of My Product Information Template Version 0.0.2");
//I'm using this as a welcome screen to inform users of new improvements on patches and other pertinent information to the users
cout << "Welcome To My Product Information Template Maker!!! \n It Is Still In Development" << endl;
cout << "\n\nworking features are:" << endl << "\t-None\n" << endl;
system("PAUSE");
system("CLS");
proinfo();
}
void proinfo()
{
//Declare Variables here
int loop(1), protype;
char make[256], model[256], year[256];
string getinput;
while(loop==1)
{
//This is the first menu the user sees where they have to choose what type
//of products users will be adding
system("CLS");
cout << "Welcome to My Product Information Template Version 0.0.0" << endl;
cout << "Please select the number of the product" << endl;
cout << "type you will be ading!\n" << endl;
cout << "1.Fe - Fe2 Moldings" << endl;
cout << "2.CF - CF2 Moldings" << endl;
cout << "What would you like to do? ";
cin >> protype;
if(protype==1)
{
//FE - FE2 Molding
system("cls");
cout << "You have selected" << endl;
cout << "Fe - Fe2 Moldings\n" << endl;
cout << "Please Enter the Make of the molding" << endl;
cin >> make;
cout << "Please Enter the Model of the molding" << endl;
cin >> model;
cout << "Please Enter the Years this molding fits" << endl;
cin >> year;
cout << "You have just created a template for a(n)" << year << " " << make << " " << model << endl;
cout << "Check My Documents For a file called Fe-Fe2template.txt" << endl;
//Asks to quit or restart
cout << "Would you like to make another tempalte?" << endl;
cin >> getinput;
if(getinput=="yes")
{
cout << "Great, Lets keep working!" << endl;
//End of If statement
}
system("PAUSE");
}
if(protype==2)
{
//CF - CF2 Molding
system("cls");
//Asks to quit or restart
cout << "Would you like to make another tempalte?" << endl;
cin >> getinput;
if(getinput=="yes")
{
cout << "Great, Lets keep working!" << endl;
//End of If statement
}
system("PAUSE");
//End of Protype Switch Statement
}
//End of while loop
}
//End of Proinfo()
}
Change year[256] to string year;
Change cin >> year; to getline(cin, year);
Add the line cin.ignore(); before the getline.
Your main problem is that the stream operator >> leaves the newline in the stream so when you try to use getline it reads an empty line. ignore() will chew up the newline and let you read the string you expect.
So this should get you on your way.
cin.ignore();
cout << "Please Enter the Years this molding fits" << endl;
getline(cin, year);
You have some other small problems but you'll figure them out. The worst is forgetting to terminate the loop.
if(getinput=="yes")
{
cout << "Great, Lets keep working!" << endl;
//End of If statement
}
else
{
loop = 0;
continue;
}