You are viewing our Forum Archives. To view or take place in current topics click here.
Need help with C++ Coding!!! Please Read!! +Rep Helpers
Posted:
Need help with C++ Coding!!! Please Read!! +Rep HelpersPosted:
Status: Offline
Joined: Jun 22, 201113Year Member
Posts: 199
Reputation Power: 7
Status: Offline
Joined: Jun 22, 201113Year Member
Posts: 199
Reputation Power: 7
Here's the code first of all:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
string answer;
cout << "Hello, what is your name?" << endl;
cout << endl;
cin >> name;
cout << endl;
cout << "Hello, " << name << " would you like to play a game." << endl;
cout << endl;
cout << "Pick 1 for yes and 2 for no." << endl;
cout << endl;
cin >> answer;
cout << endl;
int yes = 1;
int no = 2;
if (yes == 1) {
cout << "Okay, we will play a game" << endl;
cout << endl;
}
else if (no == 2) {
cout << "Okay, we won't play a game." << endl;
cout << endl;
}
else {
cout << "Sorry that was not a choice please pick yes or no." << endl;
}
How do i make it so that if i enter any number besides 1 or 2 it will say "Sorry that was not a choice."? And then it will keep asking me to choose 1 or 2 until i choose one of those numbers? Because right now i can put any number in and it will say "Okay, we will play a game." (except for 2 then it will say "Okay we won't play a game.")
Thanks!
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
string answer;
cout << "Hello, what is your name?" << endl;
cout << endl;
cin >> name;
cout << endl;
cout << "Hello, " << name << " would you like to play a game." << endl;
cout << endl;
cout << "Pick 1 for yes and 2 for no." << endl;
cout << endl;
cin >> answer;
cout << endl;
int yes = 1;
int no = 2;
if (yes == 1) {
cout << "Okay, we will play a game" << endl;
cout << endl;
}
else if (no == 2) {
cout << "Okay, we won't play a game." << endl;
cout << endl;
}
else {
cout << "Sorry that was not a choice please pick yes or no." << endl;
}
How do i make it so that if i enter any number besides 1 or 2 it will say "Sorry that was not a choice."? And then it will keep asking me to choose 1 or 2 until i choose one of those numbers? Because right now i can put any number in and it will say "Okay, we will play a game." (except for 2 then it will say "Okay we won't play a game.")
Thanks!
#2. Posted:
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
There are a few problems with your code. First of all, answer should be an integer not a string. Second, you made 2 variables called x and y. You set x equal to 1, and y equal to 2. Then right after that you check and see if x is equal to 1, and if y is equal to 2. It will be true every time, their values never change. To answer your question, you'd want to do something like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int answer;
cout << "Hello, what is your name?" << endl;
cout << endl;
cin >> name;
cout << endl;
cout << "Hello, " << name << " would you like to play a game." << endl;
cout << endl;
cout << "Pick 1 for yes and 2 for no." << endl;
cout << endl;
cin >> answer;
cout << endl;
while (answer != 1 && answer != 2)
{
cout << "You must enter either a 1 or a 2!" << endl;
cin >> answer;
}
}
- 1useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Jun 22, 201113Year Member
Posts: 199
Reputation Power: 7
Status: Offline
Joined: Jun 22, 201113Year Member
Posts: 199
Reputation Power: 7
Ok so here's the new code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int answer;
cout << "Hello, what is your name?" << endl;
cout << endl;
cin >> name;
cout << endl;
cout << "Hello, " << name << " would you like to play a game." << endl;
cout << endl;
cout << "Pick 1 for yes and 2 for no." << endl;
cout << endl;
cin >> answer;
cout << endl;
int yes = 1;
int no = 2;
while (answer != 1 && answer != 2)
{
cout << "You must enter either a 1 or a 2!" << endl;
cin >> answer;
}
system("pause");
return 0;
}
Now the only problem is that when i enter a 1 or 2 it does not say "Okay, we will/won't play a game."
So how do i fix that?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int answer;
cout << "Hello, what is your name?" << endl;
cout << endl;
cin >> name;
cout << endl;
cout << "Hello, " << name << " would you like to play a game." << endl;
cout << endl;
cout << "Pick 1 for yes and 2 for no." << endl;
cout << endl;
cin >> answer;
cout << endl;
int yes = 1;
int no = 2;
while (answer != 1 && answer != 2)
{
cout << "You must enter either a 1 or a 2!" << endl;
cin >> answer;
}
system("pause");
return 0;
}
Now the only problem is that when i enter a 1 or 2 it does not say "Okay, we will/won't play a game."
So how do i fix that?
- 0useful
- 0not useful
#4. Posted:
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
Jordannn23 wrote
Now the only problem is that when i enter a 1 or 2 it does not say "Okay, we will/won't play a game."
So how do i fix that?
After the while loop check and see what the value of answer is, and then print out the appropriate message. Also, you really shouldn't use system("pause"), it sucks.
- 1useful
- 0not useful
#5. Posted:
Status: Offline
Joined: Jun 22, 201113Year Member
Posts: 199
Reputation Power: 7
Status: Offline
Joined: Jun 22, 201113Year Member
Posts: 199
Reputation Power: 7
Okay, thanks for the help. Now i have two questions:
1. How do you condense your code into that box thing that you can click on see more or less?
2. Why shouldn't I use system("pause"); and if i don't the code that i compile won't stay on the screen the command prompt will come up for like a half second and then close if i don't use it.
I'm 14 and am just starting to learn c++ that's why i'm such a noob at this.
1. How do you condense your code into that box thing that you can click on see more or less?
2. Why shouldn't I use system("pause"); and if i don't the code that i compile won't stay on the screen the command prompt will come up for like a half second and then close if i don't use it.
I'm 14 and am just starting to learn c++ that's why i'm such a noob at this.
- 0useful
- 0not useful
#6. Posted:
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
Jordannn23 wrote Okay, thanks for the help. Now i have two questions:
1. How do you condense your code into that box thing that you can click on see more or less?
2. Why shouldn't I use system("pause"); and if i don't the code that i compile won't stay on the screen the command prompt will come up for like a half second and then close if i don't use it.
I'm 14 and am just starting to learn c++ that's why i'm such a noob at this.
1. Put code tags around it, so like [code*]YOUR CODE HERE[/code*]. Just remove the *
2. [ Register or Signin to view external links. ] There is an alternative that is way better. At the top of your code, with all the other #includes, put #include <conio.h> Then in place of system("pause"); put _getch();
- 0useful
- 0not useful
#7. Posted:
Status: Offline
Joined: Jun 22, 201113Year Member
Posts: 199
Reputation Power: 7
Status: Offline
Joined: Jun 22, 201113Year Member
Posts: 199
Reputation Power: 7
Okay, thanks so much!!!!! I repped you but you probably already knew that.
- 0useful
- 0not useful
#8. Posted:
Status: Offline
Joined: Jun 22, 201113Year Member
Posts: 199
Reputation Power: 7
Status: Offline
Joined: Jun 22, 201113Year Member
Posts: 199
Reputation Power: 7
And sorry i keep dragging this on but i thought of something else, how do i make it so it will get the first and last name not just the first name?
- 0useful
- 0not useful
#9. Posted:
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
Jordannn23 wrote And sorry i keep dragging this on but i thought of something else, how do i make it so it will get the first and last name not just the first name?
Just ask them for their last name too, lol.
- 0useful
- 0not useful
#10. Posted:
Status: Offline
Joined: Jun 22, 201113Year Member
Posts: 199
Reputation Power: 7
Status: Offline
Joined: Jun 22, 201113Year Member
Posts: 199
Reputation Power: 7
Oh, yeah haha i so stupid. Thats obvious thanks lol.
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.