Problems using cin in C++ - c++

So, I've just begun learning c++, and i've been watching some tutorials, etc. I wrote a small program that should act like a magic eight ball, however I'm having some troubles with the cin command. I've written cin >> x; where x is a string, and when the user types their question, the program is supposed to print a random response. Sounds simple enough, but if the user types more than 1 word in the question, more than 1 response is printed. So, if i type "Will I live to be 100?" I get 6 answers instead of 1. Here's my code: (I'm sure it's probably messy and not very well organized or coded in the most efficient way, like I said, I'm a beginner.)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
string z = "Yes";
string b = "Signs point to yes";
string c = "It is certain";
string d = "It is decidedly so";
string e = "Without a doubt";
string f = "Yes, definitely";
string g = "You may rely on it";
string h = "As I see it yes";
string i = "Most likely";
string j = "Outlook good";
string k = "Reply hazy try again";
string l = "Ask again later";
string m = "Better not tell you now";
string n = "Cannot predict now";
string o = "Concentrate and ask again";
string p = "Don't count on it";
string q = "My reply is no";
string r = "My sources say no";
string s = "Outlook not so good";
string t = "Very doubtful";
string u;
int main()
{
srand(time(0));
cout << "Ask A Question" << endl << endl << "Type 'Exit' to end the program" << endl << endl;
int a = 1+(rand()% 20);
cin >> u;
if (u == "Exit"){
return 0;
}
if (u == "exit"){
return 0;
}
if (a == 1){
cout << z << endl << endl;
main();
}
if (a == 2){
cout << b << endl << endl;
main();
}
if (a == 3){
cout << c << endl << endl;
main();
}
if (a == 4){
cout << d << endl << endl;
main();
}
if (a == 5){
cout << e << endl << endl;
main();
}
if (a == 6){
cout << f << endl << endl;
main();
}
if (a == 7){
cout << g << endl << endl;
main();
}
if (a == 8){
cout << h << endl << endl;
main();
}
if (a == 9){
cout << i << endl << endl;
main();
}
if (a == 10){
cout << j << endl << endl;
main();
}
if (a == 11){
cout << k << endl << endl;
main();
}
if (a == 12){
cout << l << endl << endl;
main();
}
if (a == 13){
cout << m << endl << endl;
main();
}
if (a == 14){
cout << n << endl << endl;
main();
}
if (a == 15){
cout << o << endl << endl;
main();
}
if (a == 16){
cout << p << endl << endl;
main();
}
if (a == 17){
cout << q << endl << endl;
main();
}
if (a == 18){
cout << r << endl << endl;
main();
}
if (a == 19){
cout << s << endl << endl;
main();
}
if (a == 20){
cout << t << endl << endl;
main();
}
return 0;
}

The problem is that you are calling main() over and over (recursively) in order to let the user ask another question. However, you don't take into account that std::cin stops extracting a string when it encounters a space, so you will end up making as many repetitions as there are whitespace-separated words in the user input.
Apart from this problem, the code is horrible. Sorry, it just is:
I. You declare 20 (or so) variables and a whole bunch of ifs instead of an std::vector<std::string>. This will quickly become unmaintainable if you have more elements or if you don't know the number of items.
II. You are calling main() recursively, which is legal but illegal and evil and shows a terribly bad programming style. Just don't do it. Use a loop (iterations) instead.
III. You are abusing namespace std; which is also discouraged.
IV. You are also using global variables with no good reason.
All in all, you should rewrite your program so that it reads something like this:
#include <vector>
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
std::vector<std::string> v;
v.push_back("Yes");
v.push_back("Signs point to yes");
v.push_back("It is certain");
v.push_back("It is decidedly so");
v.push_back("Without a doubt");
v.push_back("Yes, definitely");
v.push_back("You may rely on it");
v.push_back("As I see it yes");
// etc.
char q[0x100] = { 0 };
while (true) {
std::srand(std::time(nullptr));
int idx = rand() % v.size(); // this isn't perfect either, by the way
std::cout << "Ask a question:" << std::endl;
std::cin.getline(q, sizeof(q));
if (std::string(q) == "exit")
break;
std::cout << v[idx] << std::endl;
}
return 0;
}
And boom, it's just 37 lines instead of 128, and it's much readable.

cin reads a string just till the space is reached so basicly "Will I live to be 100?" contains 6 strings.
In order to avoid your problem use getline
Also you should better change your 20 string variables to a single array or vector of strings. In this case your main() will also look better as you will end up having just ONE if statemnt which will access an element of your array\vector depending on the random value.
Also you should move your global variables to main() and insert your code in a while loop which will have the following condition while (cin >> u && u != "exit") (leave only your variables declarations and srand(time(0)); outside the loop)
This answer shows how your program should actually look like.

Use getline to read a complete line

The problem here, among arguably others, is that cin stops whenever it encounters a space, and your stdin has already buffered that entire question (line), so the next time you call cin actually it reads what you had in the stdin buffer.
You should consider using getline function, that reads until a line break/carriage return.
getline(cin,u);
You should also consider learning loops, to not call main() again, and arrays for better structuring your code.

Related

An error, im trying to make a function that process a person's symptoms and then store it inside a vector string as "Y" or "N" and i get this

#include <iostream>
#include <vector>
using namespace std;
string disease(vector<string> symptom, vector<string> name, int patientnum){
string holder;
cout << "is " << name[patientnum] << " experiencing " << symptom[patientnum] << " Y/N";
cin >> holder;
if(holder == "Y" || holder == "y"){
return symptom.push_back("Y");
}
else if(holder == "N" || holder == "n"){
return symptom.push_back("N");
}
else{
cout <<"Please input Y/N" << endl;
cout <<"Program exiting...";
}
}
int main()
{
vector<string> dry, sneeze, namex;
int patientn = 0;
string input;
while(patientn < 2){
cout << "Name: ";
cin >> input;
namex.push_back(input);
disease(dry[patientn], namex[patientn], patientn);
patientn++;
}
for(patientn=0; patientn<namex.size(); patientn++){
cout << namex[patientn] << " is experiencing dry cough = " << dry[patientn] << endl;
cout << namex[patientn] << " is experiencing sneezing = " << sneeze[patientn] << endl;
}
return 0;
}
This is the error im getting
error: could not convert ‘symptom.std::vector<_Tp, _Alloc>::push_back, std::allocator > >(std::basic_string(((const char*)"Y"), std::allocator()))’ from ‘void’ to ‘std::string {aka std::basic_string}’
Im a freshmen in IT, so im sorry if my logic for making this program is stupid, and my variables naming are confusing.
So in the while loop im asking for a name and then storing it inside a name vector and then i call the function to ask for an input. "
cout <<"Is " << name << " experiecing " << symptom; //something like this but i also use an int patientnum to get the name[patientnum]
and then get y/n as the answer.
Then i make an if that accepts uppercase or lowercase answer and return symptom.push_back "Y" or "N"
and the for loop on the bottom is to print the name and then Y/N
Or can you guys suggest me a better way of doing this, im willing to learn anything.
Change the function to void, since you don't want to return anything from it.
void disease(vector<string> symptom, vector<string> name, int patientnum){
string holder;
cout << "is " << name[patientnum] << " experiencing " << symptom[patientnum] << " Y/N";
cin >> holder;
if(holder == "Y" || holder == "y"){
symptom.push_back("Y");
}
else if(holder == "N" || holder == "n"){
symptom.push_back("N");
}
else{
cout <<"Please input Y/N" << endl;
cout <<"Program exiting...";
}
}
Unfortunately that's only one of many issues with your code.
Incidentally if you spend some time choosing logical and meaningful names for your functions and variables you will find the programming easier. Funny that. Half the battle with programming is thinking clearly about what you are doing, and meaningless or arbitrary names show that you aren't thinking clearly.

Modularization in C++

I am currently working on a (very very basic) program that is a tutorial for programming( ironic given my knowledge, I know). I was instructed to modularize my code so that each unit is in its own module. I'm guessing that means adding headers? I'm working with Visual Studios, if that helps at all. I've included my code below to help my bad explanation make sense. Thanks for any help you can provide!
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int Total;
int ans;
class Question
{
private:
string Question_Text;
string Answer_one;
string Answer_two;
string Answer_three;
int Correct_Answer;
int Question_Score;
public:
void setValues(string, string, string, string, int, int);
void askQuestion();
};
int main()
{
string username = "";
char choice=' ';
char c;
int x = 4;
int y = 5;
int z = x + y;
//welcome message
cout << "Hello user, please enter your name:";
cin >> username;
cout << "Welcome to the programming tutorial " << username << "." << endl;
//menu selection
while(choice != '5')
{
cout << "What would you like to do? (Unit 1 - Declaring Variables (1), Unit 2 - Input/ Output (2), Unit 3 - Conditionals (3), Quizzes (4) or Exit (5))";
cin >> choice;
if (choice == '1')
{
cout << "We will begin with defining variables. The first step to doing this is choosing which datatype your variable is.\n";
cout << "The following are a few of the common datatypes used in programming.\n";
cout << "Character ==> char\n";
cout << "Integer ==> int, long, double\n";
cout << "Boolean ==> bool\n";
cout << endl;
cout << "When declaring a variable, you must put its datatype before the variable name.\n";
cout << "An example of this would be if we wanted to declare the value of x as 4.\n";
cout << "We would write this as: \n";
cout << "int x = 4\n";
cout << "The program will now use the value 4 for the variable name 'x'\n";
cout << endl;
cout << "Now let's assume we assigned the value of 5 to the variable 'y'\n";
cout << "If we wanted to add x and y and assign the sum to the variable 'z', we would write:\n";
cout << "int z = x + y\n";
cout << "Now when we use the variable 'z' in our program, it will perform the calculation given x=4 and y=5 and declare 9 as the value of the variable 'z'.\n";
cout << "To test our code, we would write: " << endl;
cout << "cout<<'x + y'<< z << endl; \n";
cout << "If written correctly, it will display as: \n";
cout << "x + y = " << z << "." << endl;
}
if (choice == '2')
{
cout << "Now that we understand the basics of declaring variables, let's discuss displaying, or output of, information to a user.\n";
cout << "If you wanted to display a welcome message, for example, you would type:\n";
cout << "cout << 'Welcome';\n";
cout << "The line of code would start with 'cout' followed by two less than signs and then the message you wish to display in quotes.\n";
cout << "Using this, you can ask the user for input.\n";
cout << "Enter c to continue...";
cin >> c;
cout << "Let's say we have a program that flips a coin. You may want to ask the user how many times to flip the coin.\n";
cout << "Assuming we previously declared this amount variable as 'int timesFlipped', we would 'cout' our question and the next line would read:\n";
cout << "cin>> timesFlipped; \n";
cout << "This will store the users input for the variable 'timesFlipped'\n";
cout << "You almost always end a line of code with a semi colon." << endl;
}
if (choice == '3')
{
cout << "This unit will cover conditional expressions." << endl;
}
if (choice == '4')
{
string Question_Text;
string Answer_one;
string Answer_two;
string Answer_three;
int Correct_Answer;
int Question_Score;
Question q1;
Question q2;
Question q3;
cout << username << ", you have chosen to take a quiz." << endl << endl;
int ans, score = 0;
cout << "Unit One Quiz - Variables " << endl << endl;
q1.setValues("How would you declare the value of 'x' as 12? ",
"x=12()",
"x==12()",
"x=12;()",
3,
1);
q2.setValues("What do you need to put before a variable when declaring it?",
"a name()",
"a value()",
"a datatype()",
3,
1);
q3.setValues("Which data type would you use for a number that includes a decimal value?",
"int()",
"double()",
"float()",
2,
1);
q1.askQuestion();
q2.askQuestion();
q3.askQuestion();
cout << "Your score out of a possible 3 is " << Total << endl;
}
if (choice == 'E')
{
cout << "Have a good day!";
break;
}
}
system("pause");
}
void Question::setValues(string q, string a1, string a2, string a3, int ca, int pa)
{
Question_Text = q;
Answer_one = a1;
Answer_two = a2;
Answer_three = a3;
Correct_Answer = ca;
Question_Score = pa;
}
void Question::askQuestion()
{
cout << endl;
cout << Question_Text << endl;
cout << "1. " << Answer_one << endl;
cout << "2. " << Answer_two << endl;
cout << "3. " << Answer_three << endl << endl;
cout << "Please enter your answer: " << endl;
cin >> ans;
if (ans == Correct_Answer)
{
cout << "That is correct!" << endl;
Total = Total + Question_Score;
}
else
{
cout << "Sorry, that is incorrect" << endl;
cout << "The correct answer was " << Correct_Answer << endl;
}
}
I'm guessing that means adding headers?
That's pretty much the idea.
In your case, you may want to:
Create a header name Question.h that includes the declaration of class Question.
Create a source file name Question.cpp and move the class definition there, ie all functions like void Question::askQuestion() etc.
Create a test file name test.cpp to put your main function, and remember to include the Question.h
As you are using Visual Studio, you can create a Project in advance then add all those files before compiling/building.

How create a guessing game with letters. For example I need to guess a letter from the word fallout. Which functions are useful

I am creating a guessing game. I need to ask the user to input a letter from a word like fallout. The have that letter they had inputted be correct or incorrect. I am using functions like srand(time(NULL)), rand(), psw.length. once the user inputs a letter and if they are wrong a life is deducted live--.
If they get it right they can move on to the next question with a full 5 lives. I don't know what functions I am missing if I need an array etc.
I have tried applying the rand() && psw.length together in order to at least try to randomize the letter choice so that the user might have a chance to guess the random letter from the word "fallout" but to no avail.
I have made some progress I started with the numerical portion of the code instead of focusing on the whole thing at once. Then now I have to start on the alphabetical portion of the code itself I am organizing my thoughts to simpler terms.
Now onto the alphabetical functions of the code....I now need to randomize letters for the user to answer with the correct letter of the word using functions.
I am trying to make the second answer2 = rand() % word2.length function work could anyone help me here it automatically runs the code giving a positive score to the user....
include <iostream>
#include <string>
#include <time.h>
#include <cstdlib>
using namespace std;
int lives = 3;
int guess;
int guess2;
int answer = 0;
int answer2 = 0;
int i;
int score = 0;
char letter, letter2;
string word = "fallout";
string word2 = "psw";
int main()
{
srand(time(NULL));
cout << "Welcome to the guessing game!" << endl;
cout << "*****************************" << endl;
system("PAUSE");
system("cls");
answer = rand() % 2 + 1;
lives = 3;
do {
cout << "What is a number between 1 and 2? Can you guess it in\n" << endl << lives << endl << "tries?" << endl;
cin >> guess;
if (guess == answer)
{
cout << "You won!!" << endl;
score++;
}
else if (lives == 0)
{
cout << "Your score" << endl << score;
system("PAUSE");
return 0;
}
else
{
cout << "Incorrect try again!" << endl;
lives--;
system("PAUSE");
system("cls");
}
} while (guess != answer);
cout << "You won your score is" << score << endl;
system("PAUSE");
system("cls");
answer = rand() % 3 + 1;
lives = 3;
do {
cout << "What is a number between 1 and 3? Can you guess it in" << endl << lives << "tries?" << endl;
cin >> guess;
if (guess == answer)
{
cout << "You won!!" << endl;
score++;
}
else if (lives == 0)
{
cout << "Your score" << endl << score;
system("PAUSE");
return 0;
}
else
{
cout << "Incorrect try again!" << endl;
lives--;
system("Pause");
system("cls");
}
} while (guess != answer);
cout << "You won your score is" << score << endl;
system("PAUSE");
system("cls");
answer = rand() % 5 + 1;
lives = 3;
do {
cout << "What is a number between 1 and 5? Can you guess it in\n" << endl << lives << "tries?" << endl;
cin >> guess;
if (guess == answer)
{
cout << "You won!!" << endl;
score++;
}
else if (lives == 0)
{
cout << "Your score" << endl << score;
system("PAUSE");
return 0;
}
else
{
cout << "Incorrect try again!" << endl;
lives--;
system("cls");
}
} while (guess != answer);
cout << "You won your score is " << score << endl;
system("PAUSE");
system("cls");
answer = rand() % word.length();
lives = 3;
do
{
cout << "Select the correct letter in the word '" << word << "': ";
cin >> guess;
if (guess == letter)
{
cout << "You Won!" << endl;
score++;
}
else if (lives == 0)
{
cout << "The correct answer is:" << endl;
cout << word[answer];
}
else
{
cout << "Incorrect Try Again" <<
lives--;
}
} while (guess != letter);
cout << "You won your score is " << score << endl;
system("PAUSE");
system("cls");
How can I make this code run well can anybody help me I just need advice on this function here... It keep giving the user a score++ automatically. Is their a simple fix for this. I am a rookie so if there is a basic trick here it would help!
answer2 = rand() % word2.length();
lives = 3;
do
{
cout << "Select the correct letter in the word '" << word2 << "': ";
cin >> guess2;
if (guess2 == letter2)
{
cout << "You Won!" << endl;
score++;
}
else if (lives == 0)
{
cout << "The correct answer is:" << endl;
cout << word2[answer2];
}
else
{
cout << "Incorrect Try Again" <<
lives--;
}
} while (guess2 != letter2);
cout << "You won your score is " << score << endl;
system("PAUSE");
system("CLS");
}
First of all, in C++ you have some different ways to randomize a value. rand() highly not recommended.
From cppreference:
There are no guarantees as to the quality of the random sequence produced. In the past, some implementations of rand() have had serious shortcomings in the randomness, distribution and period of the sequence produced (in one well-known example, the low-order bit simply alternated between 1 and 0 between calls). rand() is not recommended for serious random-number generation needs, like cryptography.
Instead, you can use:
#include <random>
int main() {
/*...*/
// Seed with a real random value, if available
std::random_device r;
// Choose a random mean between 1 and 6
std::default_random_engine e1(r());
std::uniform_int_distribution<int> uniform_dist(1, 7);
answer = uniform_dist(e1);
/*...*/
return 0;
}
Read more about random: https://en.cppreference.com/w/cpp/numeric/random
For loop - Condition problem: for (int i = 0; i < guess; i++) - The condition here seems wrong. Why does this loop runs until i is bigger then the user guess? I think a better way for your target is to use while loop, until the user have no lives:
int lives = 5;
size_t guess_number = 1;
/*...*/
while (lives) {
cout << "Guess" << guess_number++ << endl;
/*...*/
}
Stop the loop: Whenever the user successfully guess the letter (or the letter's place in the word), you might considering random a new letter, a new word, or just stop the game and exit the loop (with break).
The word FALLOUT: Currently, in your code, the word fallout ia a variable name, and not a variable content. start with replacing this name to something like word_to_guess, and put the value fallout into it.
string fallout;
to:
string word_to_guess = "fallout";
Now that you have done it, you can make you code more generic to another words, by choosing a random number between 1 to word_to_guess.size():
std::uniform_int_distribution<int> uniform_dist(1, word_to_guess.size());
Now you want to convert user's guess and computer's guess to letters:
/**
* guess >= 1 - The user have to guess a letter from the beginning of the word (and not before it).
* guess <= word_to_guess.size() - The user can't guess a letter that not exists in the word.
* word_to_guess[guess - 1] == word_to_guess[answer - 1] - Compare the user's letter to the computer's letter
*
* word_to_guess[answer - 1] - You might consider to replace this with word_to_guess[answer], and just random
* a number from 0 to word_to_guess.size() - 1
*/
if (guess >= 1 && guess <= word_to_guess.size() && word_to_guess[guess - 1] == word_to_guess[answer - 1]) {
cout << "You Won" << endl;
break; // Or random new letter/word etc...
}

C++ only 2 loop iterations

I'm studying at university and we started programming in C++. I had some basic concepts about Java (variables, loops and more easy things) and I tried to practice on my own with Microsoft Visual Studio, but I had a problem, this is my code, is a program that tries to guess the number you are thinking of.
void main(){
srand(time(NULL));
int number=1+rand()%100;
int highLow;
bool a;
a = true;
cout << "Think a number between 1 and 100 and I will guess it" << endl;
system("PAUSE");
cout << "\nIs it ";
cout << number;
cout << "?" << endl;
cout << "If the number is lower press 1, higher 2 and correct 3" << endl;
cin >> highLow;
while (a)
{
if (highLow == 1)
{
number = 1 + rand() % number;
cout << "\nIs it ";
cout << number;
cout << "?" << endl;
cin >> highLow;
}
else if (highLow == 2)
{
number = rand() % (100 - number+1)+number;
cout << "\nIs it ";
cout << number;
cout << "?" << endl;
cin >> highLow;
}
else if (highLow == 3)
cout << "I win this time" << endl;
a = false;}
}
The problem is that it should ask the user as many times as needed to guess the number, but it only does 2 times, then stops. Can you help me please?
If only you had indented your code properly…
The last a = false; statement executes no matter what, because it's outside the scope of the last else if statement. Basically, this:
else if (highLow == 3)
cout << "I win this time" << endl;
a = false;
means the following:
else if (highLow == 3) {
cout << "I win this time" << endl;
}
a = false;
You need to add some curly braces where appropriate.

Ending a loop once the user is wrong twice?

I am doing a quiz and testing the user. If the user is wrong he is allowed a second chance or skip, if he chooses 2nd chance and is wrong again, the game is over. How do I break out of this loop to end the game? I tried a do while loop,
do { stuff} while (wrong<2) while counting ++wrong;
every time he's wrong, but didnt work.
I have labeled the ++wrong with // statements below
void player_try (string questions[][5], char answers[])
{
char user_guess;
int m = 0;
srand(time(NULL));
int x;
int choice;
int wrong =0;
for (m=0; m<7; m++)
{
do
{
x = (rand() % 7);
cout << user_name << ": Here is question number " << m+1 << endl;
cout << m+1 << ". " << questions[x][0]<< endl;
cout << "A. " << questions[x][1]<< endl;
cout << "B. " << questions[x][2]<< endl;
cout << "C. " << questions[x][3]<< endl;
cout << "D. " << questions[x][4]<< endl;
cin >> user_guess;
user_guess = toupper(user_guess);
while (!(user_guess >= 'A' && user_guess <= 'D'))
{
cout << "Please choose a valid answer.";
cin>> user_guess;
}
if (user_guess != answers[x])
{
cout <<"Wrong!" <<endl;
++wrong; // THIS IS WHERE I COUNT WRONG ONCE
cout << "Skip this question or take a chance at greatness?" << endl;
cout << "Press 1 to skip, press 2 to take a chance at greatness" << endl;
cin >> choice;
if (choice == '1')
{
cout << "we shall skip this question." << endl;
}
else
{
cout << "I applaud your bravery." << endl;
cout << user_name << ": Here is question number " << m+1 << endl;
cout << m+1 << ". " << questions[x][0]<< endl;
cout << "A. " << questions[x][1]<< endl;
cout << "B. " << questions[x][2]<< endl;
cout << "C. " << questions[x][3]<< endl;
cout << "D. " << questions[x][4]<< endl;
cin >> user_guess;
user_guess = toupper(user_guess);
while (!(user_guess >= 'A' && user_guess <= 'D'))
{
cout << "Please choose a valid answer.";
cin>> user_guess;
}
}
if (toupper(user_guess) != answers[x])
{
cout <<"Wrong!" <<endl;
++wrong;; // THIS IS WHERE I CANT WRONG TWICE
}
else
{
cout << "correct!" << endl;
}
}
else
{
cout << "correct!" << endl;
}
}
while(wrong < 2);
}
}
Change your function return type to an integer. That simply means changing "void" to "int."
Then, inside the function place a return 0; at the point you want your function to terminate. Be sure you include another return 1; for the case that the user wins too.
This is how the main() function works. Consider:
int main()
{
string tester = "some string";
if(tester == "some string")
return 1;
cout << "Hey!"
return 0;
}
In the above case, main() terminates at the "return 1;" because the if statement was TRUE. Note that "Hey!" is never printed. It'll work the same way for your function.
As a plus, you can use that return value to let OTHER functions (such as main()) know if the function terminated because the user won (it returned 1), or lost (it returned 0).
Yes, a break statement is also a valid way to terminate the loop, but I submit that this method is the safer, cleaner way to go about it. In general, we like to know whether a function or program was successful or not.
You can use a break; statement if the person has gotten the answer wrong twice.
As per comments. You can shed the do while loop in favour of one for loop. Just put a break at the bottom if the wrong guesses are 2
There are several great suggestions for refactoring the code to remove the duplication of effort here, but to get the program functioning immediately, you've got to break out of the for loop surrounding the do { } while(wrong < 2) loop.
A simple way to do this is to modify the for loop to test the wrong variable also. The added benefit is, if I'm reading everything correctly, you'll no longer need the do{ } while(); loop.
for (m=0; m<7 && wrong < 2; m++)