-
code help
I have a large program i need help with, what my program does is makes a character and saves it as a text file i need i have a set variable for skill points here is my code (with most of it cut out for it has no relivance to my problem so ill make a small program that will have my problem in it
Code:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string spend;
int SkillPoints = 12, Switch;
cout<<" You have "<<SkillPoints<<" Points to spend do you want to spend them?(yes or no): ";
cin>>spend;
if (spend == "Yes")
{
cout<<"\n(1)magic or (2)weapons\n\n weapons cost 2 skill points magic cost 4 skill points";
cin>>Switch;
cin.ignore();
switch ( Switch )
{
case 1:
{
SkillPoints = SkillPoints - 4;
break;
}
case 2:
{
SkillPoints = SkillPoints - 2;
break;
}
default:
{
cout<<"you can only type 1 or 2: ";
cin>>Switch;
break;
}
}
cout<<"\nSpend More points? you have "<<SkillPoints<<" Points left(yes or no): ";
cin>>Switch;
cin.ignore();
}
else
{
cout<<"\nGoodbye";
cin.get();
}
return 0;
}
{
what i want it to do is when you choose magic i want it to subtract 4 from SkillPoints And then tell you you have 8 points left you can spend them on magic or weapons then subtract the right amount, when there are no more points to spend or not enough to spend i want it to print your choices ie. magic magic weapons. if i need to be clearer or put my actual program on i will its just i didn't want to fill this up with code not relivent to my problem
-
I think that the best way to do it would be to use a cascading switch statement. Here's some example code, assuming that UberStuff is 5 points, Magic is 4, and Thingy is 2.
Code:
switch(SkillPoints) {
case 5:
printf("(1) - UberStuff ");
case 4:
printf("(2) - Magic ");
case 3: case 2:
printf("(3) - Thingy ");
break;
default:
printf("You do not have enough skill points to buy anything with.\n");
}
-
what does a cascading switch statement do that a regular one doesn't?
-
In my example, if you have 5 or more points, you want to display all of the options. Since there are no breaks until the very last case, it will enter the switch at the first case, but execute the first three printf statements instead of just the first one.
-
Judging from your last response in the other thread, I misunderstood your question. Just ignore my responses here. :)
-
so it prints the options then waits for you to choose one? and pressin 1 will print (1) - Uberstuff ?
-