im trying to solve a problem in my programming book which is an incredibly simple challenge in itself, but the point is to practice passing data between functions as opposed to just writing out 3 easy lines of code to print stuff out.
i have to use one function to get the data from the user, one function to output the data and i must keep all of the data in the main function. my problem is that where the user is supposed to input their name the getline function is being skipped and its just going to the next statement, and i am ever so confused :o
please help :)
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
bool input(int& number, char name[]);
void output(int x, char y[]);
int main()
{
int number = 0;
char name[15];
//run it once to see whether or not to keep going
bool stop = input(number, name);
output(number, name);
//keep the loop going until they enter 0
while (stop)
{
cout << endl << endl;
stop = input(number, name);
output(number, name);
}
cout << endl;
return 0;
}
bool input(int& number, char name[])
{
//get the data
bool stop = false;
cout << "Number (0 to exit): " << endl;
cin >> number;
cout << "Name: " << endl;
cin.getline(name, 15, '\n');
//if they enter 0 stop the program
if (number == 0)
returnfalse;
elsereturntrue;
}
void output(int x, char y[])
{
//output the data
cout << "the number you entered is " << x;
cout << endl << "the name you entered is " << y;
return;
}