C++ programming loops, PLEASE HELP!!

Hi!! I have been trying to figure out for hours how to create a program in Visual C++ that allow to users enter 10 numbers between 10-50, and if the number entered is the same than last entry, will come up a error message.
What I got:

int n;

for(n=0;n<10;n++)
do{
System.Console.WriteLine("Enter a number")
n=int.Console(Parse.ReadLine())

}while(n<=10 || n>=50)
System.Console.WriteLine("Number incorrect")


I can not figure the last condition (if the number entered is = the last entry)
and this code show me more than 10 entries.


Can anybody help me please??
Last edited on
Declare another int lastnum, then set it equal to n if(n != lastnum)

if(n == lastnum) print the error
int n;
for(int i=0; i<10; i++)
{
do{
System.Console.WriteLine("Enter a number")
n=int.Console(Parse.ReadLine())
if( n<10 || n > 50)
{
System.Console.WriteLine("Number incorrect");
}else break;
}while(1);
//save n
}

Still not working, I have tried both of your options, here is the code, you maybe can find the error.

int n;
int lastnumber;

for (n = 0; n < 10; n++) ;


do{

System.Console.WriteLine("Enter a value from 10 to 100 inclusive");
n= int.Parse(System.Console.ReadLine());


if (n != lastnumber) ;

else

System.Console.WriteLine("Try again please");


} while ((n >= 10) && (n <= 100));

System.Console.WriteLine("Out of Range, try again");


}
}
}
I have tried put the code that you gave me and give me errors.

I have tried put another int=lastnumber, but is asking e for a value of last number,

What errors did you get?
The code is not working,
I can enter more than ten numbers, it should stop after the user enter 10 numbers, I thought for(n=0;n<10;n++) will make execute the loop only ten times but does not work.

And another condition that does not work is if the user enter the same number than the last number should comes out a message saying, "Try again please."
I define a "int lastnumber" , but the program is asking me for a value like lastnumber=0
.

Please help!!!
closed account (3qX21hU5)
Ok i'm still pretty new at C++ so my answers might be wrong, but I will try and help here.


1) Whats with the System.Console.WriteLine()? I've never used this, but it seems to me its a output system to the console. Why not just use something like this? cout << "Enter a value from 10 to 100 inclusive";


2) for(n=0;n<10;n++) should be for (int n = 0; n < 10; n++) and change int n; to something like int userinput; since you want it to store the number the user enters.



3)
1
2
3
4
int n;   // Remmber to change this
int lastnumber; 

for (n = 0; n < 10; n++) ;


You never initilized lastnumber. You need to set last number to the number the user entered at the end of the for loop.

Them are a few things that I saw didnt look to much at it just a quick skim through.

Here is a layout of one way to go solve your problem. It just a very basic template I wrote up to help you design the flow of the program alittle better, so you will need to add your own code to it. Hope it helps alittle


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
35
36
#include <iostream>

using namespace std;

int main()
{
    int usernumber = 0;
    int lastnumber = 0;

    for (int n = 0; n < 10; n++)  // This will run the loop 10 times
    {
        
        
        
        // Write the code to tell the user to enter a number between 10 and 50 here
        
        
        if (usernumber < 10 || usernumber > 50)
        {
            // This is where you put the error message telling the 
            // user that they didnt enter a number between
            // 10 - 50
        }
        else if (usernumber == lastnumber)
        {
            // Put what you want the program to do if the number is
            // the same as the last number entered here....
        }

        lastnumber = usernumber;

    } 



}


Last edited on
Please could you enclose your code in code tags? That way it's much easier to read. Like so (although the indentation has been lost):

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
int n;
int lastnumber;

for (n = 0; n < 10; n++) ;


do{

System.Console.WriteLine("Enter a value from 10 to 100 inclusive");
n= int.Parse(System.Console.ReadLine());


if (n != lastnumber) ;

else

System.Console.WriteLine("Try again please");


} while ((n >= 10) && (n <= 100));

System.Console.WriteLine("Out of Range, try again");


}
}
}


You have two loops. The first is a for loop. That is doing nothing. By putting a semicolon at the end, you're effectively saying that the only thing inside the loop is an empty statement. It's the equivalent of writing:

1
2
3
4
for (n = 0; n < 10; n++) 
{
  ;
}


The second loop is a do loop. This will keep looping as long as the condition you've given it is true. In this case, your condition is (n >= 10) && (n <= 100)

So as long as the ReadLine() call keeps returning a value between 10 and 100, it will continue looping.
So how can I write the for loop to allow the user enter 10 numbers?

int lastnumber;
int n; //number input by user//
int lastnumber; //last number input by user//

for (n = 0; n < 10; n++) ; //repeat the loop 10 times//
{

System.Console.WriteLine("Enter a value from 10 to 100 inclusive");
n = int.Parse(System.Console.ReadLine());
}

if ((n >= 10) && (n <= 50)) ; //number input by user must be between 10-50//
else
Console.WriteLine("Out of Range, try again");


while (lastnumber != n) ; //number entered must be different last number//

Console.WriteLine("Try again please");
}
}
}


when I put this code, gives me an error " use of assigned local variable ' lastnumber' ""
and it only let me enter one number!! when I take the do{ condition} out.
closed account (3qX21hU5)
Margarita see my last post it shows how to correctly use the for loop. I would also recommend that you go back the beginning and reread the textbook you are using.

I dont mean to sound harse but what I'm seeing is that you don't have a understanding of how these things work (Put some tutorials in there to if you dont have a book to work out of).

Variables - I don't think you know how variables work, and need a refresher on them. Here the link http://www.cplusplus.com/doc/tutorial/variables/

Control loops - You don't know how for loops, while loops, or any of the control structures work. Here is the link http://www.cplusplus.com/doc/tutorial/control/

Input and Output - You need to learn about the input and output streams.
Here is the link http://www.cplusplus.com/doc/tutorial/basic_io/

Them are just some of the basics I think you need to relearn. If you don't feel completely comfortable working with them keep trying. You cant just skip these because if you don't understand exactly how they work you wont understand anything else about programming.

Sorry if I sounded harse. Best of luck
Last edited on
Thank you for the advise, I am new in C++ and it is a little difficult for me.
closed account (3qX21hU5)
No problem, I was in the same boat when I started C++ also. I couldn't figure out how a lot of the things worked either. You just got to stick with it, and don't quit trying.

Another thing that helped me also was looking up stuff I didn't understand from multiple sources. Sometimes when one person explains something you might not understand it the way they are trying to teach it. So look for another source on the subject and see how they explain it.

If you need help with any of the subjects above or have any questions feel free to send me a PM on these forums and I would be glad to help.
Topic archived. No new replies allowed.