Menu

switching from numbers to letters

2002-12-09
2012-09-26
  • Nobody/Anonymous

    I know how to display letters, but i don't know how to switch form numbers to letters.  example...

    what is your name:

    what is your girlfriend's name:

    equals

    true love

    ...how do you put a name in after it asks for a name....sorry so stupid...thanks

     
    • Nobody/Anonymous

      Hmm, I may be off, and probably am, but here is a silly example I found and modified the other day that takes in and compares strings, which are used to store things like names, hopefully this will provide some hints about strings and using cin to get data into them.

      Wayne

      //Using the strcmp() function. The strcmp function compares
      //strings to see if they are the same.  It returns
      //a value of zero if the two strings are the same.

      #include<iostream>
      using namespace std;
      #include<string.h>
      #include<stdlib.h>
      int main()
      {
          //Someone could respond in any of these ways:
          char yes1[]="yes";
          char yes2[]="Yes";
          char yes3[]="YES";

          char no1[]="no";
          char no2[]="No";
          char no3[]="NO";
         
          //This input variable will hold the user's response
          char input[4];
         
          //This is the first question
          char question_1[]="\tAre you hungry? ";
          //These are the two responses to the first question
          char response_1a[]="\tThen get some food, fool!!!\n\n\n";
          char response_1b[]="\tThat's good cause there's nothing to eat.\n\n\n";

          //2nd question and its possible responses
          char question_2[] = "\tAre you mad? ";
          char response_2a[] = "\tGet some help, dude! \n\n\n";
          char response_2b[] = "\tWhatever! \n\n\n";
         
          //Start asking questions
          //Start with question 1
          cout<<question_1;
          cin>>input;
          //The use of || in C++ means OR
          if((strcmp(input, yes1)==0) ||(strcmp(input, yes2)==0) ||(strcmp(input, yes3)==0))
          cout<<response_1a;
          else
         

          if((strcmp(input, no1)==0) ||(strcmp(input, no2)==0) ||(strcmp(input, no3)==0))
          cout<<response_1b;
         
          else
          cout<<"Incorrect response."<<endl;

          //ask question #2
          cout<<question_2;
          cin>>input;
          if((strcmp(input, yes1)==0) ||(strcmp(input, yes2)==0) ||(strcmp(input, yes3)==0))
          cout<<response_2a;
          else
         

          if((strcmp(input, no1)==0) ||(strcmp(input, no2)==0) ||(strcmp(input, no3)==0))
          cout<<response_2b;
         
          else
          cout<<"Incorrect response."<<endl;
          system("pause");
      }

       
    • Curtis Sutter

      Curtis Sutter - 2002-12-09

      One thing with your program Wayne, you missed nO as an option.  My solution is toupper()  Then you only have two things to check for ('YES" and "NO") since all possibilities will become that.
      *Note* -> When dealing with a array of char you wil need a for loop or such to change each char in the array.  I do not know of a toupper() or tolower that will take a full array.

      Anyhow, you enter a name by typing it in the keyboard, then you can ask for another name (after hitting enter ofcource).
      eg.
      // Start of Prog
      #include <iostream>
      #include <ctype.h>
      #include <stdlib.h>
      using namespace std;

      int main()
      {
        system("CLS");
        char fname[50], lname[50], *pt;
       
        cout << "What is your First name?\n";
        cin >> fname;
        cin.ignore(100,'\n');
       
        cout << "What is your Last name?\n";
        cin >> lname;
        cin.ignore(100,'\n');
       
        cout << "Hello " << fname << ' ' << lname << endl;

        // Makes both name lowercase
        for (pt = fname; *pt != NULL; pt++)
          *pt = tolower(*pt);
        for (pt = lname; *pt != NULL; pt++)
          *pt = tolower(*pt);
        cout << "hello " << fname << ' ' << lname << endl;
       
        // Makes both names uppercase
        for (pt = fname; *pt != NULL; pt++)
          *pt = toupper(*pt);
        for (pt = lname; *pt != NULL; pt++)
          *pt = toupper(*pt);
       
        cout << "HELLO " << fname << ' ' << lname << endl;
       
        system("PAUSE");
        return 0;
      }
      // End of prog

      Curtis

       
    • Curtis Sutter

      Curtis Sutter - 2002-12-09

      Using the NULL in the for loops will create a warning.  You can replace the NULL with '\n' to make it go away.

      Curtis

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.