#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
usingnamespace std;
int main()
{
char selection;
do
{
cout << " Writing Data To File - \n";
cout << " ====================================\n";
cout << " 1. Start Program\n";
cout << " 2. Exit\n";
cout << " ====================================\n";
cout << " Enter your selection: ";
cin >> selection;
cout << endl;
switch (selection)
{
case'1':
cout << "Lets Begin";
cout << "\n";
break;
case'2':
cout << "Goodbye.\n";
break;
default: cout <<selection << " is not a valid menu item.\n";
cout << endl;
}
}while (selection != '2');
As you can probably see I want the program to terminate upon the user inputting the number 2 when prompted, and this works fine, however I am trying to make it so that when the user enters the number 1, the program then starts which is this next bit -
string tName, tDob, tPob; //declare a string to hold the user input
cout <<"Please enter your name: "; //prompt the user to enter a line of text
getline(cin, tName); //read in the line of text and store it in "input"
cout <<"\nPlease enter your date of birth (DD/MM/YYYY): ";
getline(cin, tDob);
cout <<"\nPlease enter your place of birth: ";
getline(cin, tPob);
cout <<"\nExit the program? y/n";
cin << exit;
//
ofstream outfile("output.txt"); // ofstream is the data type in C++ for output files / declare and open an output file called output.txt.
//print out the input string to the file with a new line.
outfile << tName << "; " << tDob << "; " << tPob << endl;
//close the output file
outfile.close();
}
As you can see, I am writing user input to a file, this isnt so much the problem, the problem im having is I dont know how to get the program to run once the user has entered the number 1.
(1) You forgot to declare "exit" as a string variable
(2) cin << exit;
Use the reverse insertion operator for cin.
(3) Your program doesnt actually end if '2' is entered, it simply continues to the second part of your code. You need to add a "return 0;" under case '2' before the break if you want it to end.
A goto statement is a quick way to solve your problem. Simply add a goto statement under case '1' before the break, and add the goto label right before the start of the program. See below:
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
usingnamespace std;
int main()
{
char selection;
do
{
cout << " Writing Data To File - \n";
cout << " ====================================\n";
cout << " 1. Start Program\n";
cout << " 2. Exit\n";
cout << " ====================================\n";
cout << " Enter your selection: ";
cin >> selection;
cout << endl;
switch (selection)
{
case'1':
cout << "Lets Begin";
cout << "\n";
goto start;
break;
case'2':
cout << "Goodbye.\n";
return 0;
break;
default: cout <<selection << " is not a valid menu item.\n";
cout << endl;
}
}while (selection != '2');
start:
string tName, tDob, tPob, exit; //declare a string to hold the user input
cout <<"Please enter your name: "; //prompt the user to enter a line of text
getline(cin, tName); //read in the line of text and store it in "input"
cout <<"\nPlease enter your date of birth (DD/MM/YYYY): ";
getline(cin, tDob);
cout <<"\nPlease enter your place of birth: ";
getline(cin, tPob);
cout <<"\nExit the program? y/n";
cin >> exit;
//
ofstream outfile("output.txt"); // ofstream is the data type in C++ for output files / declare and open an output file called output.txt.
//print out the input string to the file with a new line.
outfile << tName << "; " << tDob << "; " << tPob << endl;
//close the output file
outfile.close();
}
Thanks for the help guys! My only problem now is when I run the program and select option 1, it throws up all the questions when they should appear one by one, how do I fix this :/ sorry to be a pain im a real noob with c++!
Also, what would I have to do to the program to so that when the user has finished entering the data, the program willoutput a copy of what is in the
file? (read from the file)
This might sound like a bit of a bad answer, but there is plenty of material out there! Many people have already asked these questions and many people have given them solutions. Find them and interpret them into your own code as your OWN solution. That will help you develop better skills and can be pretty rewarding when YOU figure out what to do with what is given.