Getting a program to either terminate or re-run based on user input

Hi everyone,
I'm still fairly new to c++ and am having issues getting my program to perform another calculation, or to terminate based on user input.
I'd really appreciate the help..
Below is my current code:

#include <iostream>
using namespace std;

void yes();
void no();

void no()
{
cout<<"Thank You!"<<endl;
}
void yes()
{
goto main;
main:;
}

int main()
{

char yes='y';
char no='n';
char ans= yes ||no;

int a[10];
int bin;
int i;


cout<<"Enter the number to convert: ";
cin>>bin;

for(i=0; bin>0; i++);


a[i]=bin%2;
bin= bin/2;


cout<<"Binary of the given number= ";
for(i=i-1 ;i>=0 ;i--)

cout<<a[i];


cout<<"Do you want to perform another calculation? "<<endl;
cin>>ans;

if (ans=='y')

void yes();


else

void no();

return 0;
}

goto is not a good option.

just use a loop:
do
{
...
cin >> ans;
...

} while( ans != 'n')


what happens is that you hit your goto main: which takes the current execution there. The next statement is the end of the function, so it does that. the function yes() ends. Main now ends when yes() returns. C++ isnt totally linear in this way: it calls a function, then it goes back to where it was, so your goto has no effect, it just goes to the end of yes() but in main, its still on the line calling yes(), and proceeds from there.
it may work if you put main: label at the top of main, but I forget how c++ handles a goto outside its scope, and it may not like it.

the best goto alternative is: (but again, this is highly disliked by the c++ community and best practices: you should not loop via goto when a loop will do the job)
main()
{
main:
... code
cin >> ans;
if(ans == 'y')
goto main;
}
Last edited on
I'll try that, thanks
Hello SeanDolla1999,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



The extra comments are not meant to stay, but help for now.
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//  Getting a program to either terminate or re-run based on user input SeanDolla1999 271422

#include <iostream>

using namespace std;

// <--- Since the functions are compiled before they are called these prototypes are not needed.
//void yes();
//void no();

void no()
{
    cout << "Thank You!" << endl;
}

void yes()  // <--- Pointless function. It does nothing.
{
    return;   // <--- And you do not even need this.
//    goto main;
//main:;
}

int main()
{

    char ans{};  // <--- Only 1 is needed. Initialized to '\0'.
    //char no = 'n';
    //char ans = yes || no;

    int a[10]{};
    int bin{};
    //int i;  // <--- should be defined in the for loop unless you would need it outside the for loop. Which it does not look like you do.


    cout << "Enter the number to convert: ";
    cin >> bin;  // <--- Does this mean I have to enter 1s and 0s? Are you looking for a hex number or a decimial?

    for (int i = 0; bin > 0; i++);  // <--- Does this have a purpose?


    a[i] = bin % 2;  // <--- Should these lines be part of the for loop?
    bin = bin / 2;


    cout << "Binary of the given number= ";
    for (int i = i - 1; i >= 0; i--)
    {
        cout << a[i];
    }

    cout << "Do you want to perform another calculation? ";  // <--- No "endl" or "n" will put the "cin" on this line not the next.
    cin >> ans;

    //if (ans == 'y')
    //{
    //    void yes();
    //}
    if (ans == 'n')  // <--- But what if 'N' was typed?
    {
        void no();
    }

    return 0;
}

As the comments in the "yes" function show you could have written this:
1
2
3
void yes()  // <--- Pointless function. It does nothing.
{
}

Evan though a "goto" should never be used there are rare occasions when it is useful. To that the label "main:" ends with a colon. The semi-colon is not needed.

But since the function does nothing useful it is not needed at all. And the way that you have used the "goto" it has no effect or causes any problems.

More often a do/while or while loop is used to continue the program. I tend to use a do/while here because the code will execute once before the while condition is checked.

Lines 36 - 61 would comprise the do while loop and you could still keep lines 58 - 61.

There are several ways to write the while condition:
} while (ans != 'n');. Cross your fingers and hope the user does what you want.
} while (ans != 'n' && ans != 'N');. This checks for both cases. Usually when there is a "!=" on both sides the ( && ) is needed. On occasion you will need ( || ). I always end up testing it to find out which one works, usually it is the ( && ).
Lastly } while (std::tolower(ans) == 'n');. This will change the case if needed and you will have only 1 check to make. The opposit is "std::toupper()". This function will need the header file "cctype" for these function and other useful functions.

Andy
Topic archived. No new replies allowed.