You are viewing our Forum Archives. To view or take place in current topics click here.
Please help!!!!!!!!!!!!!!!!!!!!!!!!!
Posted:
Please help!!!!!!!!!!!!!!!!!!!!!!!!!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
When I choose 1 to count by fives for some reason it also counts by ten. But when I choose 2 it only counts by tens. What do I need to change so that when I choose 1 it only counts by fives?
Here's the code:
#include <iostream>
#include <string>
using namespace std;
string name;
int main()
{
int choice, x, i;
cout << "Hello what is your name?" << endl;
cout << endl;
cin >> name;
cout << endl;
cout << "Hello " << name << endl;
cout << endl;
cout << "Would you like to play a counting game?" << endl;
cout << endl;
cout << "If you want to count by fives press 1." << endl;
cout << endl;
cout << "If you want to count by tens press 2." << endl;
cout << endl;
{
cin >> choice;
cout << endl;
if (choice == 1)
{
cout << "Okay we will count forward by fives." << endl;
cout << endl;
for (int i = 0; i <= 50; i += 5)
{
cout << i << " ";
}
}
else if (choice == 2)
cout << "Okay we will count forward by tens." << endl;
cout << endl;
for (int i = 0; i <= 50; i += 10)
{
cout << i << " ";
}
}
system ("pause");
return 0;
}
Here's the code:
#include <iostream>
#include <string>
using namespace std;
string name;
int main()
{
int choice, x, i;
cout << "Hello what is your name?" << endl;
cout << endl;
cin >> name;
cout << endl;
cout << "Hello " << name << endl;
cout << endl;
cout << "Would you like to play a counting game?" << endl;
cout << endl;
cout << "If you want to count by fives press 1." << endl;
cout << endl;
cout << "If you want to count by tens press 2." << endl;
cout << endl;
{
cin >> choice;
cout << endl;
if (choice == 1)
{
cout << "Okay we will count forward by fives." << endl;
cout << endl;
for (int i = 0; i <= 50; i += 5)
{
cout << i << " ";
}
}
else if (choice == 2)
cout << "Okay we will count forward by tens." << endl;
cout << endl;
for (int i = 0; i <= 50; i += 10)
{
cout << i << " ";
}
}
system ("pause");
return 0;
}
#2. 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
Come on someone please help me! I'm a very beginner.
- 0useful
- 0not useful
#3. Posted:
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
So for the not answering the PM, out for my B/Day lol
Simple one this, you have not encapsulated the if statement
else if (choice == 2)
cout << "Okay we will count forward by tens." << endl;
cout << endl;
for (int i = 0; i <= 50; i += 10)
{
cout << i << " ";
}
If you put the {} like this
else if (choice == 2)
{
cout << "Okay we will count forward by tens." << endl;
cout << endl;
for (int i = 0; i <= 50; i += 10)
{
cout << i << " ";
}
}
then it should work ;)
Simple one this, you have not encapsulated the if statement
else if (choice == 2)
cout << "Okay we will count forward by tens." << endl;
cout << endl;
for (int i = 0; i <= 50; i += 10)
{
cout << i << " ";
}
If you put the {} like this
else if (choice == 2)
{
cout << "Okay we will count forward by tens." << endl;
cout << endl;
for (int i = 0; i <= 50; i += 10)
{
cout << i << " ";
}
}
then it should work ;)
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.