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.
// 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:
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 [B]do you want to repeat?: n[/B] Press any key to continue . . .
Output when I enter 'y' or any other character. The program goes crazy:
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 [B]do you want to repeat?: y [COLOR="red"]enter your name: how many times you want to print?:[/COLOR][/B]


Sign In
Create Account


Back to top









