code help

This is a discussion on code help within the C++ Programming forums, part of the General Programming Boards category; I have a large program i need help with, what my program does is makes a character and saves it ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    176

    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

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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");
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    what does a cascading switch statement do that a regular one doesn't?

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Judging from your last response in the other thread, I misunderstood your question. Just ignore my responses here.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    176
    so it prints the options then waits for you to choose one? and pressin 1 will print (1) - Uberstuff ?

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 09:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21