Input conversion from char to int

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • qwerty6523
    New Member
    • Aug 2007
    • 2

    Input conversion from char to int

    this is my code its only the beginning

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    int main(int argc, char *argv[])
    {
    int a;
    loop6:
    cout << "what key do u wish to use: ";
    cin >> a;
    if (a == 1)
    goto loop;
    else if (a == 2)
    goto loop2;
    else if (a == 3)
    goto loop3;
    else if (a == 4)
    goto loop4;
    else if (a == 5)
    goto loop5;
    else
    goto loop6;
    loop:

    loop2:

    loop3:

    loop4:

    loop5:
    system("PAUSE") ;
    return EXIT_SUCCESS;
    }

    each key they go to will be a different code

    what this program is supossed to do is when they type in a sentence it converts each letter to a number and each key will convert the letters to different numbers.
    my problem is a dont know how to scan the sentence they type in for a certain letter.

    thanks in advance

    i hope you understand the question
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Please read about the switch statement and the while/for loop statement. If
    Dijkstra would've still been alive he would've defenestrated your for using all
    these goto statements: "Go To Considered Harmful".

    kind regards,

    Jos

    Comment

    • qwerty6523
      New Member
      • Aug 2007
      • 2

      #3
      int main(int argc, char *argv[])
      {
      int a;
      cout << "what key do u choose: ";
      cin >> a;
      do {

      } while (a == 1);
      do {

      } while (a == 2);

      do {

      } while (a == 3);

      do {

      } while (a == 4);

      do {

      } while (a == 5);
      do {

      } while (a == 6);
      system("PAUSE") ;
      return EXIT_SUCCESS;
      }
      is this better

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by qwerty6523
        int main(int argc, char *argv[])
        {
        int a;
        cout << "what key do u choose: ";
        cin >> a;
        do {

        } while (a == 1);
        do {

        } while (a == 2);

        do {

        } while (a == 3);

        do {

        } while (a == 4);

        do {

        } while (a == 5);
        do {

        } while (a == 6);
        system("PAUSE") ;
        return EXIT_SUCCESS;
        }
        is this better
        Well, if you just need to do something once depending on a then should replace the do while loops with if statements. Or you could use a switch:
        [code=cpp]
        switch (a)
        {
        case 1:
        break;
        case 2:
        break
        ... and so on ...
        [/code]
        Also you can use a do while to make sure the input is what you want:
        [code=cpp]
        do {
        cin << a;
        } while (a != 1 && a != 2 && a != 3 && a != 4 && a != 5 && a != 6);
        [/code]

        Comment

        Working...