Reputation: 469
I need help with some code, what i want it to do is be able to make the computer 'beep' using the alert function ie: \a but I don't know how to implement it so that the user can choose how many times it beeps while using a switch statement, All help will genuinely be thanked.
#include <iostream>
using namespace std;
int main()
{
int x;
int y;
cout << "Do you want to make your computer beep" << endl;
cin >> x;
if (x == 'y' || x == 'Y')
{
cout << "How many beeps do you want" << endl;
switch (y)
{
// This is the part i'm stuck on!!!
}
}
cin.ignore();
cin.get();
return 0;
}
Upvotes: 0
Views: 2760
Reputation: 1574
Using switch you will never finish writting your program, there are y infinite possibilities! (Well, in fact there are –2147483648 to 2147483647) Use a for loop instead:
#include <iostream>
using namespace std;
int main()
{
int x;
int y;
cout << "Do you want to make your computer beep" << endl;
cin >> x;
if (x == 'y' || x == 'Y')
{
cout << "How many beeps do you want" << endl;
for(int i = 0; i < y; i++)
{
cout << "\a";
}
}
cin.ignore();
cin.get();
return 0;
}
The "for" loop uses i as a counter and it's incremented 1 unit for each iteration, starting by 0, when i = y the loop ends, so you are doing from 0 to (y-1) beeps, counting the 0 beep the sum of all is y, and that is what you want :)
Upvotes: 0
Reputation: 4978
You probably shouldn't use the switch to do that otherwise you will have to write a case for every number that they could possibly select. A for loop should be used here:
int n;
cout << "How many beeps? " << endl;
cin >> n;
for (int i = 0; i < n; i++) {
cout << "\a";
}
Upvotes: 5