For loops

I'm trying to create a program that prompts the user to enter a number between 1-10 and they get 10 tries to guess right. I've made progress, however I am a bit stuck. If they guess right it is supposed to say "correct", if not it should say "try again" for 9 more times. Its trying 11 times, it doesn't say correct, and I'm not sure I'm using the For loop properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include <iostream>
using namespace std;

    int main()
{
    int my_guess = 5;
    int user_guess = 0;

    cout << "Guess a number between 1 and 10.\n You will have up to 10 tries" <<endl;
    cin >> user_guess;

    for (int i =1; i <= 10 && (my_guess != user_guess); i++)

{
    if (user_guess == my_guess)
        {

        cout << "Correct" <<endl;
        }
    else
    {
{
        cout << "Try again" <<endl;
        cin >> user_guess;
}}
}




        return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include <iostream>
using namespace std;

    int main()
{
    int my_guess = 5;
    int user_guess = 0;

    cout << "Guess a number between 1 and 10.\n You will have up to 10 tries" <<endl;
    cin >> user_guess;

    for (int i =1; i <= 10 && (my_guess != user_guess); i++)

{
    if (user_guess == my_guess)
        {

        cout << "Correct" <<endl;
        };
    else
    {
        cout << "Try again" <<endl;
        cin >> user_guess;
     };
};




        return 0;

}

I think this one should be your code. You had to many paranthesses.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;

int main()
{
    int number, guess = 5;

    cout << "Enter a number between 1 and 10: ";

    for (int i = 0; i < 10; i++)
    {
        if (i != 0)
        {
            cout << "Try again." << endl;
        }

        cin >> number;

        if (number == guess)
        {
            cout << "You won!";
            return 0;
        }
    }

    cout << "You lose.";

    return 1;
}
Last edited on
The second one looks good, the only problem is I'm supposed to practice the For loops......I'm just having a hard time grasping them.
I'm sorry; I forgot. I have edited my post.
Josue, Cool, that looks way easier than I was trying to make it. I'm gonna play with this a bit. Thank you
Hey, you can try using a do-while loop, just suggesting.
I don't know those yet, however when I do I'm sure I'll need help then........I did just figure out this though, an it's not working just right. It's just adding my first and last numbers.....any help?

#include<iostream>
using namespace std;

int main()

{

int total = 0, num = 0, sum = 0;

cout << "Enter a positive number between 1 and 10" <<endl;
cin >> total;

cout << "Now we're going to add that many numbers together.....\n Enter first number" <<endl;

for (num = 1; num <= total; num++)
{
if (num !=1)
{
cout << "Enter next number" <<endl;
}
cin >> num;

}
sum = num + sum;
cout << "The sum of your numbers are:" << sum <<endl;

return 0;
}




Looking at your code mate, i think you're really just always going to get the sum as (1 + the last value that was entered by the user), that's because the program is not actually 'using' or 'storing' the other values that are entered by the user because the value of num keeps changing, and in two ways

1. The next value entered by the user becomes the new value of num
2. The "num++" increments the previous value entered by the user

The compiler is confused, i suggest you use a different variable as your 'loopcounter variable' i.e the variable used in the 'for loop' ,and use your 'num' variable to keep the values entered by the user, that way, the compiler won't be too confused.

Next, you really want to be adding the values entered as soon as they are entered by the user, you cannot first enter all the values and then add them all up, because you are only using one variable ('num'), and it can only store one value at a time, so all the other values entered are actually lost,

I HOPE i'm not confusing you

So something like this should worK;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>

using namespace std;

int main()
{
   int total = 0, num = 0, sum = 0;

   cout << "Enter a positive number between 1 and 10" <<endl;
   cin >> total;

   cout << "Now we're going to add that many numbers together.....\n Enter first number" <<endl;

   for (int loopcounter /*or anything else*/ = 1; loopcounter <= total; loopcounter++)
   {
      if (loopcounter /*or what you chose*/ !=1)
      {
         cout << "Enter next number" <<endl;
      }
      cin >> num;
      sum += num;   // OR just use "sum = num + sum;" , it's really the same
    }   

   cout << "The sum of your numbers are:" << sum <<endl;

   return 0;
   }
Topic archived. No new replies allowed.