help me pls.

can anybody help me with this :

(consecutive numbers)Write a C++ application that lets the user to input a sequence of consecutive numbers.
In other words, the program should let the user keep entering numbers as long as the current number is one greater than the previous number, and if the user enters a wrong number the program must display an error message.

please help me .:(
Would it kill you to at least start it?

Regardless, I like these simple things so here you go!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std

int main()
{
	int input, i = 1;
	while (true)
	{
		cout << endl << "Enter the number " << i << " :";
		cin >> input;
		if (input != i) cout << endl << "Error, try again.";
		else i++;
	}
}
Last edited on
i am so sorry, im not familiar yet with programming. :((( hmm, thats only the start right?
thank you so much !!! you've helped me a lot :)))))
The output of the code I gave you would output:

Enter the number 1 : 1
Enter the number 2 : 2
Enter the number 3 : 2
Error, try again.
Enter the number 3 : 3
Enter the number 4 : 4
etc...


I believe this fits the requirements of your initial question. For your code above you cout the menu options but you never get an input. Then you say if(a==a) which will always be true so you are going to get all of the outputs.

Here are a few other practice tips. Declare int a in the main function instead of making it global. At the end you use system("pause") which is not recommended because it is OS specific, slow, and vulnerable to backdoors. Use cin.get(); instead.

So, to fix your code above, try this:

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
#include <iostream>
using namespace std;

int main()
{
	int a, MenuIn;	//Moved 'a' here and declared another int MenuIn.
	do{	//Your do-while would only repeat outputs, this lets the user also change the input.
		cout<<"Enter a number:";
		cin>>a;

		cout<<endl << "1. Add 1"; //You will want to end lines or they will be printed one after another
		cout<<endl << "2. Multiply by 2";
		cout<<endl << "3. Subtract 4" <<endl << "4. Quit"<<endl; //You can use one cout if you like for different lines, just seperate them with 'endl'
		cin >> MenuIn; //You need to let the user input something here.


		if(a==1)cout<<"The sum is:"<<a+1<<endl;
		if(a==2)cout<<"The product is:"<<a*2<<endl;
		if(a==3)cout<<"The difference is:"<<a-3<<endl;
		if(a==4)break;  //You could return, but break is a little nicer and will get you out of your loop.  Otherwise you wont access your cin.get()
	}while(a<5);  //Your-do while loop didn't change 'a' and so it would repeat forever it it didn't return (which it would), 

	cin.get();  //Don't use system("anything")
	
	return 0;
}
Last edited on
now, i know my errors. thank you so much ! your great ! :)
Topic archived. No new replies allowed.