Jump to content
  • Advertisement
Sign in to follow this  
Delsana

C++ Case Statement Problem

This topic is 4161 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm doing a program where you enter information and it recalls it back at your command, so far it worked with the main way of doing it with if and else but no matter what it would always choose one thing, so for the first selection if you pressed a it wouldn't error, but the others would.. So I changed that into a case statement and that seemed to work and besides the fact that I can't remember where I should insert a neat line of "Please enter your selection" it seemed to work until I tryed letters, since it can only do constants thats not working, I tried changing the String to an Int but had little success with that.
Code:


                          // The purpose of the project is to create a data
                          // selection in which people can recall specific pieces
                          // of data that they previously inputed

#include <iostream>
#include <string.h>

using namespace std;      // Prevents the need to type std for many commands

int main()
{
    string first;
    cout << "Enter your First Name: ";    // Records the information that is inputed
    getline (cin, first);                 // reports your input back to you
    cout << " Your First Name is: " << first << "\n";

    string last;
    cout << "\nEnter your Last Name: ";     // Records the information that is inputed
    getline (cin, last);                  // reports your input back to you
    cout << " Your Last Name is: " << last << "\n";

    string address;
    cout << "\nWhat is your Address? ";     // Records the information that is inputed
    getline (cin, address);               // reports your input back to you
    cout << " Your Address is: " << address << "\n";

    string city;
    cout << "\nWhat city do you live in? "; // Records the information that is inputed
    getline (cin, city);                  // reports your input back to you
    cout << " You live in the city of " << city << "\n";

    string state;
    cout << "\nWhich state do you live in? "; // Records the information that is inputed
    getline (cin, state);                   // reports your input back to you
    cout << " You live in the state of " << state << "\n";

    string zipcode;
    cout << "\nWhat is your zipcode? ";   // Records the information that is inputed
    getline (cin, zipcode);             // reports your input back to you
    cout << " Your zipcode is " << zipcode << "\n";

    string age;
    cout << "\nHow old are you? ";    // Records the information that is inputed
    getline (cin, age);             // reports your input back to you
    cout << " You are " << age << " years old " << "\n";

    string school;
    cout << "\nWhat school do you go to? "; // Records the information that is inputed
    getline (cin, school);                // reports your input back to you
    cout << " You go to the " << school << "\n";

    string graduation;
    cout << "\nWhat year will you graduate? "; // Records the information that is inputed
    getline (cin, graduation);               // reports your input back to you
    cout << " Your year of graduation is " << graduation << "\n";

    string college;
    cout << "\nWhat college do you plan to go to? "; // Records the information that is inputed
    getline (cin, college);                        // reports your input back to you
    cout << " You're planning to go to the " << college << "\n";

    enum Information { Ifirst, Ilast, Iaddress, Icity, Istate,       // Saves the information
                       Izipcode, Iage, Ischool, Igraduation, Icollege, // inputed it an array
                       InputData };

    string ArrayInput[InputData] = { first, last, address, city, state,
                                  zipcode, age, school, graduation, college };

    cout << "\n";
    cout << ArrayInput[Ischool] << "\n";


    cout << "\n";

    cout << "What information would you like to retrieve? " << "\n";
    cout << "First Name = 1 " << "\n";
    cout << "Last Name = 2 " << "\n";
    cout << "Address = 3 " << "\n";
    cout << "City = 4 " << "\n";
    cout << "State = 5 " << "\n";
    cout << "Zipcode = 6 " << "\n";
    cout << "Age = 7 " << "\n";
    cout << "School = 8 " << "\n";
    cout << "Graduation = 9 " << "\n";
    cout << "College = 10 " << "\n";
    cout << "Quit = 11 " << "\n";





     string flag_end_session = "N";
     int info;
     do {
     cin >> info;

int a;
char szInput [6];        // failed attempt at string to int transfer
gets ( szInput );
a = atoi (szInput);

     switch ( a )



     {
      case 0:
         cout << "\n";
         cout << "Error, Invalid Entry";
         cout << "\n";
         cout << "Please enter new data: ";
         break;

      case 1:
         cout << "\n";
         cout << "Records show the result is";
         cout << " ";
         cout << ArrayInput[Ifirst];
         cout << "\n";
         cout << "Please enter new data: ";
         break;

      case 2:
         cout << "\n";
         cout << "Records show the result is";
         cout << " ";
         cout << ArrayInput[Ilast];
         cout << "\n";
         cout << "Please enter new data: ";
         break;

      case 3:
         cout << "\n";
         cout << "Records show the result is";
         cout << " ";
         cout << ArrayInput[Iaddress];
         cout << "\n";
         cout << "Please enter new data: ";
         break;

      case 4:
         cout << "\n";
         cout << "Records show the result is";
         cout << " ";
         cout << ArrayInput[Icity];
         cout << "\n";
         cout << "Please enter new data: ";
         break;

      case 5:
         cout << "\n";
         cout << "Records show the result is";
         cout << " ";
         cout << ArrayInput[Istate];
         cout << "\n";
         cout << "Please enter new data: ";
         break;

      case 6:
         cout << "\n";
         cout << "Records show the result is";
         cout << " ";
         cout << ArrayInput[Izipcode];
         cout << "\n";
         cout << "Please enter new data: ";
         break;

      case 7:
         cout << "\n";
         cout << "Records show the result is";
         cout << " ";
         cout << ArrayInput[Iage];
         cout << "\n";
         cout << "Please enter new data: ";
         break;

      case 8:
         cout << "\n";
         cout << "Records show the result is";
         cout << " ";
         cout << ArrayInput[Ischool];
         cout << "\n";
         cout << "Please enter new data: ";
         break;

      case 9:
         cout << "\n";
         cout << "Records show the result is";
         cout << " ";
         cout << ArrayInput[Igraduation];
         cout << "\n";
         cout << "Please enter new data: ";
         break;

      case 10:
         cout << "\n";
         cout << "Records show the result is";
         cout << " ";
         cout << ArrayInput[Icollege];
         cout << "\n";
         cout << "Please enter new data: ";
         break;

      case 11:
         cout << "\n";
         cout << "Ending Session";
         cout << "\n";
         cout << "Have a nice day";
         flag_end_session = "Y";
         cout << "\n";
         break;

      default:
         cout << "\n";
         cout << "Error, Invalid Entry";
         cout << "\n";
         cout << "Please enter new data: ";
         break;
     }
     } while ( flag_end_session == "N" );


     return 0;
}



ALSO This is the first attempt at the program which worked fine except in the first entry since it would accept anything.

                          // The purpose of the project is to create a data
                          // selection in which people can recall specific pieces
                          // of data that they previously inputed

#include <iostream>
#include <string.h>

using namespace std;      // Prevents the need to type std for many commands

int main()
{
    string first;
    cout << "Enter your First Name: ";    // Records the information that is inputed
    getline (cin, first);                 // reports your input back to you
    cout << " Your First Name is: " << first << "\n";

    string last;
    cout << "\nEnter your Last Name: ";     // Records the information that is inputed
    getline (cin, last);                  // reports your input back to you
    cout << " Your Last Name is: " << last << "\n";

    string address;
    cout << "\nWhat is your Address? ";     // Records the information that is inputed
    getline (cin, address);               // reports your input back to you
    cout << " Your Address is: " << address << "\n";

    string city;
    cout << "\nWhat city do you live in? "; // Records the information that is inputed
    getline (cin, city);                  // reports your input back to you
    cout << " You live in the city of " << city << "\n";

    string state;
    cout << "\nWhich state do you live in? "; // Records the information that is inputed
    getline (cin, state);                   // reports your input back to you
    cout << " You live in the state of " << state << "\n";

    string zipcode;
    cout << "\nWhat is your zipcode? ";   // Records the information that is inputed
    getline (cin, zipcode);             // reports your input back to you
    cout << " Your zipcode is " << zipcode << "\n";

    string age;
    cout << "\nHow old are you? ";    // Records the information that is inputed
    getline (cin, age);             // reports your input back to you
    cout << " You are " << age << " years old " << "\n";

    string school;
    cout << "\nWhat school do you go to? "; // Records the information that is inputed
    getline (cin, school);                // reports your input back to you
    cout << " You go to the " << school << "\n";

    string graduation;
    cout << "\nWhat year will you graduate? "; // Records the information that is inputed
    getline (cin, graduation);               // reports your input back to you
    cout << " Your year of graduation is " << graduation << "\n";

    string college;
    cout << "\nWhat college do you plan to go to? "; // Records the information that is inputed
    getline (cin, college);                        // reports your input back to you
    cout << " You're planning to go to the " << college << "\n";

    enum Information { Ifirst, Ilast, Iaddress, Icity, Istate,       // Saves the information
                       Izipcode, Iage, Ischool, Igraduation, Icollege, // inputed it an array
                       InputData };

    string ArrayInput[InputData] = { first, last, address, city, state,
                                  zipcode, age, school, graduation, college };

    cout << "\n";
    cout << ArrayInput[Ischool] << "\n";


    cout << "\n";

    cout << "What information would you like to retrieve? " << "\n";
    cout << "First Name = 1 " << "\n";
    cout << "Last Name = 2 " << "\n";
    cout << "Address = 3 " << "\n";
    cout << "City = 4 " << "\n";
    cout << "State = 5 " << "\n";
    cout << "Zipcode = 6 " << "\n";
    cout << "Age = 7 " << "\n";
    cout << "School = 8 " << "\n";
    cout << "Graduation = 9 " << "\n";
    cout << "College = 10 " << "\n";
    cout << "Quit = 11 " << "\n";


    int userselection;         // integrates the new looping and if system
    do {                     // activates a requirement and action if the fields

    cout << "\n";            // are successfully met
    cout << "Please enter your selection: ";
    cin >> userselection;

    if  (userselection == 11)
    {
    cout << "\nProgram is now exiting";
    cout << "\nThank You";
    }
    else
    {
    cout << "Recalling your information: ";
    cout << ArrayInput[userselection -1];
    cout << "\n";
    }
    } while ( userselection < 11 );

    return 0; // Ends the entire sequence of coding


[Edited by - Delsana on February 1, 2007 7:27:11 AM]

Share this post


Link to post
Share on other sites
Advertisement
1. Use [ source ][ /source ] tags (without the spaces) to format your code so that it is readable.

2. Note that there's a 'For Beginners' forum at the top of the forums page. Questions related to basic syntax and program logic should go there (and in fact this thread will probably be moved).

3. Try to describe your problem in more specific terms. Is it a run-time error? Compile-time? What are the error messages? To what lines of code do they refer?

Share this post


Link to post
Share on other sites
There is no error message simply that on the first listed sequence of coding the case statements work perfectly (except that I can't remember to where to put the "Please enter your selection") until you enter a letter then the choice just repeats itself again and again and again and again until it freezes the entire program.

For the second one the program works perfectly except that for the first entry if you enter a letter which would normally come out as an error, it will read it. Oh and it also appears the second one for some reason I guess some of the code got cut out, but now it doesn't even read the letters as errors, but even when it used to it would allow the first entry in the program to be a letter and it would not pull up the proper error.

Share this post


Link to post
Share on other sites
Its not really a beginner error, but whatever..

I suppose what I need is a way to make the first program allow letters to be entered without any repetition, and will read as an error.

And for the second program I need a way to make it so that the program will read letters as an error, give an error message, and allow it so that no matter what if you enter a letter for the first "Enter your selection" it will result in an error instead of a response.

Share this post


Link to post
Share on other sites
Quote:
Original post by Delsana
Its not really a beginner error, but whatever..
Questions regarding basic logical flow, reading and processing user input, control structures, and so on are usually most at home in For Beginners. It's no slight to you, but moving posts to For Beginners where appropriate helps keep the other forums free of clutter and more on-topic.

Now, somehow your stream operators got turned into HTML codes (at least that's what I see on Firefox); this happens occasionally, and to be honest I'm not sure why. It's hard to read though, so perhaps you could try posting the code once more. You might just go ahead and put it in a new post rather than editing your previous post. (Also, the indentation is still inconsistent, so perhaps you could clean that up a little as well.)

I see a number of things in your code that need to be addressed, but it'll be much easier to provide feedback and pinpoint the cause of the problem you're describing once the code's a little more presentable.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!