Repeat code

I have finally created my first script in C++ completely alone and it works! But there is one thing that i want to do but dont know how to.

I want the option to restart the program and run through the entire script again from the start if Enter is pressed.

How can i do this?

This is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
	int x = 0;
	int y = 0;
	cout << "Choose a number: ";
	cin >> x;
	system("cls");
	cout << "Choose a second number: ";
	cin >> y;
	system("cls");
	int sum = x + y;
	cout << "The sum of " << x << " and " << y << " is " << sum << endl;

	cin.clear();
	cin.ignore(255, '\n');
	cin.get();
	return 0;
}
You could try wrapping it inside a do while loop:

http://www.cplusplus.com/doc/tutorial/control/

there is a section that covers "Iteration Structures (loops)" in this tutorial.

Just out of curiosity do you have to use Enter to repeat the code or can you use a (yes/no) prompt?

Hope this helps.
You could use a do/while loop. And check that against '13' Which is a carrage return ( Enter ).

1
2
3
4
5
6
7
do
{
    code...

    char vari = _getch();

}while( vari == 13 );


to use _getch(), #include <conio.h>

Here's the ascii table:
http://penguin.dcs.bbk.ac.uk/academic/networks/introduction/units/ascii-full.gif
Last edited on
I added your code into mine, but i get errors. I may have added it wrong though.

Here is my code.

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()
{
	do
	{
		int x = 0;
		int y = 0;
		cout << "Choose a number: ";
		cin >> x;
		system("cls");
		cout << "Choose a second number: ";
		cin >> y;
		system("cls");
		int sum = x + y;
		cout << "The sum of " << x << " and " << y << " is " << sum << endl << endl;

		cin.clear();
		cin.ignore(255, '\n');
		cin.get();
		return 0;

		char vari = _getch();
	};
	while( vari == 13 );


This is the error.

1>------ Build started: Project: CMD, Configuration: Debug Win32 ------
1> CMD.cpp
1>c:\users\daniel\documents\visual studio 2010\projects\cmd\cmd\cmd.cpp(24): error C3861: '_getch': identifier not found
1>c:\users\daniel\documents\visual studio 2010\projects\cmd\cmd\cmd.cpp(25): error C2059: syntax error : ';'
1>c:\users\daniel\documents\visual studio 2010\projects\cmd\cmd\cmd.cpp(26): error C2065: 'vari' : undeclared identifier
1>c:\users\daniel\documents\visual studio 2010\projects\cmd\cmd\cmd.cpp(26): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
did you add the header file, conio.h?

#include <conio.h>


Also, before char vari = _getch(); you may want to add a cout, so the user knows to press enter to re-run the code or any other key to exit.
Last edited on
Oh sorry i missed that bit. After adding it i get these errors.

1>------ Build started: Project: CMD, Configuration: Debug Win32 ------
1> CMD.cpp
1>c:\users\daniel\documents\visual studio 2010\projects\cmd\cmd\cmd.cpp(26): error C2059: syntax error : ';'
1>c:\users\daniel\documents\visual studio 2010\projects\cmd\cmd\cmd.cpp(27): error C2065: 'vari' : undeclared identifier
1>c:\users\daniel\documents\visual studio 2010\projects\cmd\cmd\cmd.cpp(27): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
before the while(...) remove the ; you have, after the }
That fixed two more errors, but it still says vari is undeclared and something about a '{'.


1>------ Build started: Project: CMD, Configuration: Debug Win32 ------
1> CMD.cpp
1>c:\users\daniel\documents\visual studio 2010\projects\cmd\cmd\cmd.cpp(27): error C2065: 'vari' : undeclared identifier
1>c:\users\daniel\documents\visual studio 2010\projects\cmd\cmd\cmd.cpp(28): fatal error C1075: end of file found before the left brace '{' at 'c:\users\daniel\documents\visual studio 2010\projects\cmd\cmd\cmd.cpp(6)' was matched
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
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
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
	do
	{
		int x = 0;
		int y = 0;
		cout << "Choose a number: ";
		cin >> x;
		system("cls");
		cout << "Choose a second number: ";
		cin >> y;
		system("cls");
		int sum = x + y;
		cout << "The sum of " << x << " and " << y << " is " << sum << endl << endl;

		cin.clear();
		cin.ignore(255, '\n');
		cin.get();
		return 0;   //You're ending your program here!!!!!! Move this down to where I have it

		char vari = _getch();
	}while( vari == 13 );

        return 0;
}
Last edited on
Oh i thought the program ended at the end of the code. Anyway, all of the errors are gone but it is still saying that vari is undeclared.
return 0; causes the program to return an exit code from main, which ends the program.

declare 'vari' before the do/while loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char vari = 0;         //declare here

	do
	{
		int x = 0;
		int y = 0;
		std::cout << "Choose a number: ";
		std::cin >> x;
		system("cls");
		std::cout << "Choose a second number: ";
		std::cin >> y;
		system("cls");
		int sum = x + y;
		std::cout << "The sum of " << x << " and " << y << " is " << sum << std::endl << std::endl;

		std::cin.clear();
		std::cin.ignore(255, '\n');
                //I removed the cin.get() here, it's unneeded now.

		vari = _getch();          //get key pressed here
	}while( vari == 13 );



EDIT:
Scrap the declaring of that and just add it to the while statement, lol.
while( _getch() == 13 )
Last edited on
Topic archived. No new replies allowed.