C++ Case Problems

Started by
1 comment, last by kiome 17 years, 2 months ago
Here is the C++ Code, and I'm trying to get it to be able to use letters and give an error when there used, unfortunately the conversion from string to int didn't work, and I've had no success with fixing this.
                        
 // 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];
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;
}


As for this work, I need to be able to make it report a letter as an error, and find a way to make it so if a letter is used for the first recall selection it will report it as an error.

                        // 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
}

Advertisement
um... any help would be appreciated.
Glancing over your code I see two things wrong:
Include <string> instead of <string.h>, SC++L headers don't have a .h ending.

The second thing is, you use atoi to convert string to integer, which is a C function.
In order to convert a string into an integer and check for it's validity in C++, you should use a stringstream.

#include <iostream>#include <string>#include <sstream>int main(){        int my_int;	std::string my_string;	std::cin >> my_string;                std::stringstream ss;	ss << my_string;	ss >> my_int;	if ( ss.fail() ) { std::cout << "Input was not a number!"; }	else             { std::cout << my_int; }			return 0;}
My Blog

This topic is closed to new replies.

Advertisement