Switch case help ?

This is a discussion on Switch case help ? within the C++ Programming forums, part of the General Programming Boards category; Hi all this is my code Code: #include <iostream.h> #include <string.h> void prompt1(); int prompt2(); void greeting(); int main() { ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    15

    Switch case help ?

    Hi all this is my code
    Code:
                #include <iostream.h>
    #include <string.h>
    
    void prompt1();
    int  prompt2();
    void greeting();
    
    int
    main()
    {
      prompt1();
      switch (prompt2()) {
      case 1:
        cout<<"Well? ";
        greeting();
        break;
      case 2:
        cout<<"Meanie"<<endl;
        break;
      default:
        cout<<"Huh?"<<endl;
        break;
      }
    
      return 0;
    }
    
    void
    prompt1()
    {
      char answer[50];
    
      cout<<"How are you? ";
      cin.getline(answer, 50);
      cout<<"You are: "<< answer <<endl;
    }
    
    int
    prompt2()
    {
      int answer;
    
      cout<<"Aren't you going to ask how I am?\n";
      cout<<"1) Yes\n2) No\n: ";
      cin>> answer;
      cin.ignore();
    
      return answer;
    }
    
    void
    greeting()
    {
      char greeting[50];
    
      cin.getline(greeting, 50);
      if (strcmp(greeting, "How are you?") != 0) {
        cout<<"You still haven't asked me how I am!"<<endl;
      }
      else {
        cout<<"I'm fine, thank you."<<endl;
      }
    }
    When i have typed "1" it says well then i type something and the box closes i've tried using multiple cin.get;'s but not much seems to work anyone got any ideas?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    When all else fails, try this:
    Code:
    int
    main()
    {
      prompt1();
      switch (prompt2()) {
      case 1:
        cout<<"Well? ";
        greeting();
        break;
      case 2:
        cout<<"Meanie"<<endl;
        break;
      default:
        cout<<"Huh?"<<endl;
        break;
      }
      char ch;
      while (cin.get(ch) && ch != '\n') {
        // Nothing goes here
      }
      cin.get();
    
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    15
    plz may u put that in english lol ?!?!i copy and pasted it and it said it had 9 errors!! all i want it to do is for it to stay open and deliver the final message

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >plz may u put that in english lol ?!?!
    English doesn't compile.

    >i copy and pasted it and it said it had 9 errors!!
    You probably didn't integrate the code properly (translation: You cut and pasted wrong). This is the code that works for me and should work for you as well.
    Code:
    #include <iostream.h>
    #include <string.h>
    
    void prompt1();
    int  prompt2();
    void greeting();
    
    int
    main()
    {
      prompt1();
      switch (prompt2()) {
      case 1:
        cout<<"Well? ";
        greeting();
        break;
      case 2:
        cout<<"Meanie"<<endl;
        break;
      default:
        cout<<"Huh?"<<endl;
        break;
      }
      char ch;
      while (cin.get(ch) && ch != '\n') {
        // Nothing goes here
      }
      cin.get();
    
      return 0;
    }
    
    
    void
    prompt1()
    {
      char answer[50];
    
      cout<<"How are you? ";
      cin.getline(answer, 50);
      cout<<"You are: "<< answer <<endl;
    }
    
    int
    prompt2()
    {
      int answer;
    
      cout<<"Aren't you going to ask how I am?\n";
      cout<<"1) Yes\n2) No\n: ";
      cin>> answer;
      cin.ignore();
    
      return answer;
    }
    
    void
    greeting()
    {
      char greeting[50];
    
      cin.getline(greeting, 50);
      if (strcmp(greeting, "How are you?") != 0) {
        cout<<"You still haven't asked me how I am!"<<endl;
      }
      else {
        cout<<"I'm fine, thank you."<<endl;
      }
    }
    p.s. iostream.h is an antiquated header. If your compiler doesn't support the following program, I highly recommend you get a new compiler so that I don't have to keep dumbing down my code so that it'll compile for you.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int
    main()
    {
      cout<<"Hello, world!"<<endl;
    }
    If that program does compile and run, please start using the newer headers as just about everyone here will be giving you code in modern C++.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 09:42 AM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 01:07 AM

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