You are viewing our Forum Archives. To view or take place in current topics click here.
Hey I'm 14 and trying to learn c++ and I need help Please!
Posted:

Hey I'm 14 and trying to learn c++ and I need help Please!Posted:

Jordannn23
  • Challenger
Status: Offline
Joined: Jun 22, 201113Year Member
Posts: 199
Reputation Power: 7
Status: Offline
Joined: Jun 22, 201113Year Member
Posts: 199
Reputation Power: 7
I'm trying to run a program so that after I get the user's name and stuff that I'm asking if he wants to play a game. So I am trying to get it so if the user types 1 then the it will say "Okay we will play a game."

But if the user types 2 then it will say "Okay we won't play a game."
Please help me like i said I am very new. Please don't be mean lol.

Here's my code:

#include <iostream>
#include <string>

using namespace std;
string name;
string answer;

int main()
{
int count, x;
count = x;
x = 1;
int count, i;
count = i;
i = 2;
cout << "Hello what is your name? " << endl;
cin >> name;
cout << endl;
cout << "Hello " << name << endl;
cout << "Would you like to play a game? " << endl;
cout << "If you want to play a game press 1. " << name << endl;
cout << "If you don't want to play a game press 2. " << name << endl;
cin >> answer;
cout << endl;
while (x=1) {
cout << "Okay we will play a game. " << name << endl;
x++;

cout << "Count is " << count << "." << endl;
}
{
while (i=2) {
cout << "Okay we won't play a game. " << name << endl;
i++;

cout << "Count is " << count << "." << endl;
}
}

system ("pause");

return 0;
}
#2. Posted:
Bring
  • Ladder Climber
Status: Offline
Joined: Dec 23, 201112Year Member
Posts: 379
Reputation Power: 20
Status: Offline
Joined: Dec 23, 201112Year Member
Posts: 379
Reputation Power: 20
I will edit this post when I'm done.

EDIT:
while (x=1) {
cout << "Okay we will play a game. " << name << endl;
x++;

cout << "Count is " << count << "." << endl;
}

It has to be:
while (x==1) {
cout << "Okay we will play a game. " << name << endl;
x++;

cout << "Count is " << count << "." << endl;
}

X = 1 means that you are setting x to 1;
X == 1 means that you are comparing them...
Fix this for the rest of your code too!



-DanielDaniel
#3. Posted:
Jordannn23
  • Challenger
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.
#4. Posted:
Imp
  • Blind Luck
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Let's look at the code in sections

int count, x;
count = x;
x = 1;
int count, i;
count = i;
i = 2;

Your variables are a little mixed up all these are a little unnecessary
So remove them

The next line is waiting for the answer 1 or 2

cin >> answer;

This is what you need to check to see what the answer is so instead of

while (x=1) {
cout << "Okay we will play a game. " << name << endl;
x++;

cout << "Count is " << count << "." << endl;
}
{
while (i=2) {
cout << "Okay we won't play a game. " << name << endl;
i++;

cout << "Count is " << count << "." << endl;
}
}

All you need is

If (answer == "1")
{
cout << "Okay we will play a game. " << name << endl;
}

If (answer == "2")
{
cout << "Okay we won't play a game. " << name << endl;
}

cout << "Count is " << answer << "." << endl;


This should give the right answer. I haven't tested so apologys if some errors come up. I am on my phone!
#5. Posted:
Jordannn23
  • Challenger
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 thx but I have changed my code up a little so instead of while loops I put in if else statements so please check this.

#include <iostream>
#include <string>

using namespace std;
string name;

int main()
{
int choice, x, i;
cout << "Hello what is your name? " << endl;
cin >> name;
cout << endl;
cout << "Hello " << name << endl;
cout << "Would you like to play a game? " << endl;
cout << "If you want to play a game press 1. " << name << endl;
cout << "If you don't want to play a game press 2. " << name << endl;
cout << (1) << "Okay we will play a game. " << name << endl;

{
cout << (2) << "Okay we won't play a game. " << name << endl;

cin >> choice;
cout << endl;

if (choice == 1)
{
cout << "Okay we will play a game." << endl;
}
else if (choice == 2)
{
cout << "Okay we won't play a game." << endl;
}

system ("pause");

return 0;
}
#6. Posted:
Imp
  • Retired Staff
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
That looks a lot better!

Maybe worth thinking about what happens if they dont press 1 or 2

There is a way you can label your question then if the answer isn't acceptable then it can ask it again

If you have trouble pm me always willing to help!
#7. Posted:
XG_R4PiDzZ
  • Junior Member
Status: Offline
Joined: Sep 28, 201014Year Member
Posts: 82
Reputation Power: 3
Status: Offline
Joined: Sep 28, 201014Year Member
Posts: 82
Reputation Power: 3
Jordannn23 wrote


Keep things simple. This should be simple enough, but as Rango said, think about if another number/key is entered.
#include <iostream>

int main()
{
    int choice;
    std::string name;

    std::cout << "What's your name?" << std::endl;

    std::cin >> name;

    std::cout << std::endl << "Would you like to play a game " << name << "?" << std::endl;
    std::cout << "[1] Yes" << std::endl;
    std::cout << "[2] No" << std::endl;

    std::cin >> choice;

    if (choice == 1)
        std::cout << std::endl << "Playing!" << std::endl;
    else if (choice == 2)
        std::cout << std::endl << "Not Playing." << std::endl;
}
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.