Subroutines

I wish to create a 'naming' application. You submit a name and are asked whether this is correct or not. If yes, the program proceeds, if not, I want the naming procedure to start again. I believe this is done by the use of subroutines, but I cannot for the life of me figure out how. Step by step instructions, or else a link to the correct keyword to use would be greatly appreciated.
Feel free to ask if something needs clearing up! Sorry for my foolishness.

Here is the current code:


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

void main(){
cout << "Welcome to Strontium!" << endl;
string nameAnswer;
string playerName;
cout << "What is your name? " << endl;
cout << "> ";
getline(cin, playerName);
cin.clear();
cout << "Your name is " << playerName;
cout << "? Yes/No." << endl;
cout << "> ";
cin >> nameAnswer;


if(nameAnswer == "yes"){
cout << "Very good. Thank you.";}
if(nameAnswer == "no"){
Here is where I wish the program to return to 'string nameAnswer;' and follow on from there. However, I don't know whether the code needs to be within a .h file, or a separate .cpp, or whether I can just use a keyword!
}
}

_getch();
}



Thank you!

This is what you need. You wanted a loop to help you, not a subroutine. Subroutines will come in handy too, but for different reasons.

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

int main ()
{
	string playerName;
	char nameAnswer;
	

	do 
	{
		cout << "Welcome to Strontium!" << endl;
		cout << "What is your name? " << endl
			<< "> ";
		cin >> playerName;
		cout << "Your name is " << playerName;
		cout << "? Please Confirm, type Y or N." << endl
			 << "> ";
		cin >> nameAnswer;
		cin.ignore(256,'\n');
	}
	while (toupper(nameAnswer) != 'Y');
	
	cout << "Very good. Thank you.";

	return 0;
}



Edit - why do you need conio.h? Start adding some C++ libraries, they will make things easier on you.
Last edited on
Flaming marvellous! Thank you so much! I've been tearing my hair out trying to do this. In regards to conio.h; I've bee following a set of tutorials and they advise to include it, so it's just become habit. *Shrug*. Thank you again!
Topic archived. No new replies allowed.