[try Beta version]
Not logged in

 
How can I go to beginning and repeat code?

Dec 16, 2013 at 5:39pm
I'm new to C++, and I made a simple code to multiply two numbers, but I don't know how to return to the beginning to repeat the code without restarting it. Should I use "goto"?

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

using namespace std;

void main()
{
	int x;
	cout<<"x=";
	cin>>x;

	int y;
	cout<<"y=";
	cin>>y;
	cout<<endl;

	cout<<x;
	cout<<"*";
	cout<<y;
	cout<<"=";
	cout<<x*y;
	cout<<""<<endl;
	cout<<""<<endl;

	system ("pause");
}
Dec 16, 2013 at 5:49pm
Should I use "goto"?
- No!

Read this - http://www.cplusplus.com/doc/tutorial/control/
Dec 16, 2013 at 5:52pm
main() generally has int type. If you want to go back to beginning, I can think of two ways. First, is using "goto". Second is using a while loop. You can also include a conditional statement at the end (before system("pause")) to exit the program when you wish. I'll give you an example of this. I apologize for not taking time to align brackets.
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
37
38
39
40
41
42
#include <iostream>

using namespace std;

int main()
{
        int a = 1; //Defined an int of type 
        while (a>0)
        {
	int x;
	cout<<"x=";
	cin>>x;

	int y;
	cout<<"y=";
	cin>>y;
	cout<<endl;

	cout<<x;
	cout<<"*";
	cout<<y;
	cout<<"=";
	cout<<x*y;
	cout<<""<<endl;
	cout<<""<<endl;
        int exit_status;
        cout << "enter 0 to exit and 1 to continue " << endl;
        cin >> exit_status;
        if (exit_status == 0)
        { 
               break;
         }
         else             //not needed but whatever.
         {
                continue;
         }
            a++;
         }

	system ("pause");
        return 0;
}
Dec 16, 2013 at 6:03pm
Thank you so much. Btw, Viraldude, how can I clear the screen after returning to the beginning?
Dec 16, 2013 at 6:14pm
I believe the command line for that is system("CLS");
I was checking some other forums and found this. This might be better since it was posted by pretty well known people on this forums.
cout << string(50, '\n');
I hope it helps.
Last edited on Dec 16, 2013 at 6:16pm
Dec 16, 2013 at 6:22pm
Thank you.
Dec 16, 2013 at 11:51pm
In this code, where would I put
 
system("CLS");

or
 
cout<<string(50, '\n');

Thanks.



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
37
38
39
40
41
42
#include <iostream>

using namespace std;

int main()
{
        int a = 1;
        while (a>0)
        {
	int x;
	cout<<"x=";
	cin>>x;

	int y;
	cout<<"y=";
	cin>>y;
	cout<<endl;

	cout<<x;
	cout<<"*";
	cout<<y;
	cout<<"=";
	cout<<x*y;
	cout<<""<<endl;
	cout<<""<<endl;
        int exit_status;
        cout << "enter 0 to exit and 1 to continue " << endl;
        cin >> exit_status;
        if (exit_status == 0)
        { 
               break;
         }
         else
         {
                continue;
         }
            a++;
         }

	system ("pause");
        return 0;
}
Dec 17, 2013 at 12:00am
Where do you want to clear the screen? I assume you want to clear the screen after you decide to continue, so place it after that check.

By the way, continue skips everything after it in the loop. Also, incrementing a in that loop is pointless. As long as a is non-zero, your loop condition will be true.
Dec 17, 2013 at 1:36am
Okay so there are a lot of things going on in this post that shouldn't be. Suggestions are being made that are absolutely wrong..

If you want to loop you have a few options:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// For loops
for(initialization; condition; increment/decrement)
{
...
}

//While loop
while(condition)
{
...
}

//Do-While Loop
do
{
...
}while(condition)


Please use one of these.

Second, never use system() for anything unless your professor requires it. If he doesn't, don't use it.

http://www.cplusplus.com/articles/j3wTURfi/


There are multiple posts about clearing your screen, please use search:

http://www.cplusplus.com/articles/4z18T05o/

And lastly.. Don't use system().
Dec 17, 2013 at 2:18am
Thank you so much. And this isn't for a professor, Protomega. I just wanted to learn C++. It's definitely so much more complicated than calculator programming.
Dec 17, 2013 at 2:48am
Hahaha yes, but calculator programming is definitely an easy way to get sucked into the programming world but when you begin to learn about Object Oriented Programming you will have a hard time going back to the calculator because of how limited it is and your mind may not be able to switch back to a non-"dynamic" language. Welcome to the world of C++ by the way.
Dec 17, 2013 at 3:03am
Retarded question here but can you use recursion with int main()? Like if he wanted to go back to the beginning could he write:
1
2
3
4
5
6
int main ()
{
    //code related things
   main ();
   return 0;
}
Dec 17, 2013 at 3:08am
Disallowed by the standard and produces undefined behaviour. Don't do it.
Dec 17, 2013 at 3:14am
@Sargon94
You can definitely do that but C++ doesn't recommend you doing it. It also eats up memory. Also, what will the main function return here? I guess you were trying to ask
1
2
3
4
5
int main ()
{
     //code
     return main();
}

Please correct me if I am wrong.
Dec 17, 2013 at 3:18am
You're not wrong, I was just curious, 4 years of programming and I'd never considered calling the main for any reason. Why does it eat up memory? Does re-declare variables without them passing out of scope somehow?
Dec 17, 2013 at 3:29am
It should, if I'm right just keep on adding to the stack without ever pulling away from the stack. Causing memory issues.
Topic archived. No new replies allowed.