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:
System.Console.WriteLine("Enter a number")
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
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.
1 2 3 4
int n; // Remmber to change this
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
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
elseif (usernumber == lastnumber)
// Put what you want the program to do if the number is
// the same as the last number entered here....
lastnumber = usernumber;
for (n = 0; n < 10; n++) ;
System.Console.WriteLine("Enter a value from 10 to 100 inclusive");
if (n != lastnumber) ;
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.
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.
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.