issue with loops

HI
I have a small program with 3 files.
main.cpp

#include "head.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
loop:
string name;
cout << "what is your name " << endl;
getline (cin, name);
if (name == "mark")
next();
else;
cout << "invalid input " << endl;
goto loop;

}

next.cpp
#include <iostream>
#include <string>
#include "head.h"
using namespace std;

int next()
{
cout << "woo hoo I did it " << endl;

}

and head.h
#ifndef HEAD_H
#define HEAD_H

int next();

#endif

My problem is when I enter my name as mark the out put is

"woo hoo i did it"
"invalid input"
"what is your name"

I want the program to end after the call to next.cpp
Thanks
your else statement should be else, the semicolon is killing it

EDIT:

This might help

main.cpp
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
#include "head.h"
#include <iostream>
#include <string>
using namespace std;

int main() {

        // NEVER use goto, use a while loop instead. This basically says, if true is true, then loop... aka, infinite loop.
        while (true) {

                string name;
                cout << "what is your name " << endl;
                getline (cin, name);

                // Using curly braces with logic and control statement help to make the code more clear

                if (name == "mark") {

                        next();

                        // Break out of infinity
                        break;

                } else { 

                        cout << "invalid input " << endl;

                }

        }

}
Last edited on
1
2
3
4
5
else
{
    cout << "invalid input " << endl;
    goto loop;
}
Thanks more great answers from you guys welldone
Topic archived. No new replies allowed.