I had an idea for a program that asked the user how many 'participant's they had and then asked them to input 'values' for each of those participants. Due to my 'n00bish' nature the only solution I have would have me writing case scenarios until the end of time. Please save me for this awful fate and suggest an alternative to an eternity of typing.
#include <iostream>
#include <string>
#include <cmath>
int main(void) {
using std::cout;
using std::cin;
using std::string;
int casescenario;
float a;
float b;
float c;
cout << "Please enter number of participants";
cin >> casescenario;
switch (casescenario) {
case 1:
cout << "you have one participant\n";
cout << "enter the value of that participant";
cin >> a;
cout << "you participant's value is" << a;
case 2:
cout << "you have two participants\n";
cout << "enter value for participant 1";
cin >> a;
cout << "enter value for participant 2";
cin >> b;
cout << (a+b)<< "\n" << "Is the value of your participant's\n";
break;
case 3:
cout << "you have three participants\n";
cout << "enter value for participant 1";
cin >> a;
cout << "enter value for participant 2";
cin >> b;
cout << "enter value for participant 3";
cin >> c;
break;
}
system ("pause");
return 0;
}
The switch statement obviously was never meant to handle this kind of scenario but I'm sure there is an alternative. I greatly appreciate any help on this. I am currently working on a book to learn C++ but I'm adopting these side projects to keep things fresh and address my curiousity.
#include <iostream>
usingnamespace std;
int main()
{
int casescenario;
int x(1),y;
cout<<"Enter the number of participants\n";
cin>> casescenario;
float a[casescenario];
for(x=1;x<=casescenario;x++)
{
cout<<"Enter value for participant "<<x<<":";
y=x-1;
cin>>a[y];
}
}
By the way, 'n00bish' doesn't mean you do not have a brain. Try to think. I am also a beginner.Best of Luck.
Sorry if you thought the question was a by-product of thoughtlessness, I had been contemplating the problem and a loop never occurred to me to be a viable option but I stand corrected on that issue. Considering that I have never encountered loops in my reading, I am also not sure how to utilze the values that are entered for the 'participants'. Suppose I wanted to add all those values together...how might I do that?
Here's a little piece of advice, for learning computer science: Ask yourself "what happens if I generalize this?". Though the problem here seems to be more that you simply haven't read enough, if you haven't seen loops yet. Loops are a pretty basic thing...
The answer to that is another loop:
1 2 3 4
int sum = 0;
for(int i=0;i<casescenario;i++){
sum += a[i];
}