Hi
What's the wrong with the code? When I enter 'n' it exits fine. But when I enter 'y' or any other character the program goes crazy. Please have look at the outputs at the bottom.
Code:
// print your name n times
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
void prntname(int dummy1, string dummy2);
int main()
{
string name; int n; char ch;
do
{
cout << "enter your name: ";
getline (cin, name);
cout << "how many times you want to print?: "; cin >> n;
prntname(n, name);
cout << endl;
cout << "do you want to repeat?: "; cin >> ch;
}
while (ch != 'n');
cout << endl;
system("pause");
return 0;
}
//---------------------------------------------------------
// function definition for void prntname(int dummy, string dummy)
void prntname(int dummy1, string dummy2)
{
for (int j=1; j<=dummy1; j++)
{
cout << dummy2 << endl;
}
}
//------------------------------------------------------------
Output when I enter 'n'. The program exits fine:
Code:
enter your name: jackson heights
how many times you want to print?: 10
jackson heights
jackson heights
jackson heights
jackson heights
jackson heights
jackson heights
jackson heights
jackson heights
jackson heights
jackson heights
do you want to repeat?: n
Press any key to continue . . .
Output when I enter 'y' or any other character. The program goes crazy:
Code:
enter your name: jackson heights
how many times you want to print?: 10
jackson heights
jackson heights
jackson heights
jackson heights
jackson heights
jackson heights
jackson heights
jackson heights
jackson heights
jackson heights
do you want to repeat?: y
enter your name: how many times you want to print?: