Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Determining whether input is string or integer

Determining whether input is string or integer

Scheduled Pinned Locked Moved C / C++ / MFC
jsonhelpquestion
12 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Klazen
    wrote on last edited by
    #1

    I have a program that allows the user to input variables into a program, perform some math on the variables, and then spit out the answer. However, if the user enters a string, rather than a number, the program skips the rest of the input sequence and goes straight to the math. I'm trying to avoid this using a try...catch statement. However, I cannot run any code to check the error, because as soon as the user presses enter, the rest of the input code is skipped, without allowing me to run any code to test it. Is there any way around this whithout simply telling the user to be careful and only enter numbers?

    L D C L 4 Replies Last reply
    0
    • K Klazen

      I have a program that allows the user to input variables into a program, perform some math on the variables, and then spit out the answer. However, if the user enters a string, rather than a number, the program skips the rest of the input sequence and goes straight to the math. I'm trying to avoid this using a try...catch statement. However, I cannot run any code to check the error, because as soon as the user presses enter, the rest of the input code is skipped, without allowing me to run any code to test it. Is there any way around this whithout simply telling the user to be careful and only enter numbers?

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Klazen wrote:

      Is there any way around this

      Several options come to mind. 1) Re-read the material provided for your course 2) Ask your instructor for some guidance 3) Network with other students in your class 4) Drop the course and change your Major

      led mike

      K 1 Reply Last reply
      0
      • L led mike

        Klazen wrote:

        Is there any way around this

        Several options come to mind. 1) Re-read the material provided for your course 2) Ask your instructor for some guidance 3) Network with other students in your class 4) Drop the course and change your Major

        led mike

        K Offline
        K Offline
        Klazen
        wrote on last edited by
        #3

        Thanks for the sarcasm, but it's not an assignment. I'm simply trying to learn more about C++. Also, just FYI, I'm still in high school. Haven't even chosen a major.

        L 1 Reply Last reply
        0
        • K Klazen

          I have a program that allows the user to input variables into a program, perform some math on the variables, and then spit out the answer. However, if the user enters a string, rather than a number, the program skips the rest of the input sequence and goes straight to the math. I'm trying to avoid this using a try...catch statement. However, I cannot run any code to check the error, because as soon as the user presses enter, the rest of the input code is skipped, without allowing me to run any code to test it. Is there any way around this whithout simply telling the user to be careful and only enter numbers?

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Klazen wrote:

          Is there any way around this whithout simply telling the user to be careful and only enter numbers?

          int x;
          std::cin >> x;

          "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          K 1 Reply Last reply
          0
          • D David Crow

            Klazen wrote:

            Is there any way around this whithout simply telling the user to be careful and only enter numbers?

            int x;
            std::cin >> x;

            "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            K Offline
            K Offline
            Klazen
            wrote on last edited by
            #5

            That's what I am doing (well actually, I've got "using namespace std" and the top of the file, so I don't have to write that all over the place.) But when I type "s", for instance, it skips the rest of the "cin" statements and moves on to the code after it.

            D 1 Reply Last reply
            0
            • K Klazen

              I have a program that allows the user to input variables into a program, perform some math on the variables, and then spit out the answer. However, if the user enters a string, rather than a number, the program skips the rest of the input sequence and goes straight to the math. I'm trying to avoid this using a try...catch statement. However, I cannot run any code to check the error, because as soon as the user presses enter, the rest of the input code is skipped, without allowing me to run any code to test it. Is there any way around this whithout simply telling the user to be careful and only enter numbers?

              C Offline
              C Offline
              cp9876
              wrote on last edited by
              #6

              I'd recommend Stroustrup's book on C++ (title "The C++ Programming Language"), one of his first examples is a calculator which reads numbers and expressions from the input and shows one way to parse it and extract numbers, symbols, labels etc..

              Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

              1 Reply Last reply
              0
              • K Klazen

                That's what I am doing (well actually, I've got "using namespace std" and the top of the file, so I don't have to write that all over the place.) But when I type "s", for instance, it skips the rest of the "cin" statements and moves on to the code after it.

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                Did you see this?

                "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                1 Reply Last reply
                0
                • K Klazen

                  I have a program that allows the user to input variables into a program, perform some math on the variables, and then spit out the answer. However, if the user enters a string, rather than a number, the program skips the rest of the input sequence and goes straight to the math. I'm trying to avoid this using a try...catch statement. However, I cannot run any code to check the error, because as soon as the user presses enter, the rest of the input code is skipped, without allowing me to run any code to test it. Is there any way around this whithout simply telling the user to be careful and only enter numbers?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  have a look at boost's lexical cast[^] The underlying implementation uses string streams (which is pretty slow) so you could do something similar yourself if you dont want to use their library something along the lines of this... stringstream ss; string input; int value; try { ss << input; ss >> value; } catch(cant remember exactly what is thrown) { //if you get here its not an int }

                  G 1 Reply Last reply
                  0
                  • L Lost User

                    have a look at boost's lexical cast[^] The underlying implementation uses string streams (which is pretty slow) so you could do something similar yourself if you dont want to use their library something along the lines of this... stringstream ss; string input; int value; try { ss << input; ss >> value; } catch(cant remember exactly what is thrown) { //if you get here its not an int }

                    G Offline
                    G Offline
                    Gregory Bryant
                    wrote on last edited by
                    #9

                    start everything out using a char varable char number[100] then us a static_cast to turn it in to a int if the user enters a charcter they going to get sumthing crazy but other then that you could use if or catch statements

                    C C 2 Replies Last reply
                    0
                    • G Gregory Bryant

                      start everything out using a char varable char number[100] then us a static_cast to turn it in to a int if the user enters a charcter they going to get sumthing crazy but other then that you could use if or catch statements

                      C Offline
                      C Offline
                      Cedric Moonen
                      wrote on last edited by
                      #10

                      Gregory Bryant wrote:

                      start everything out using a char varable char number[100] then us a static_cast to turn it in to a int if the user enters a charcter they going to get sumthing crazy but other then that you could use if or catch statements

                      :omg: What ?? Are you kidding ? You mean you want to use a static_cast on the char array to convert it to an integer ? Oh man, that is just plain wrong.

                      Cédric Moonen Software developer
                      Charting control [v1.2]

                      1 Reply Last reply
                      0
                      • K Klazen

                        Thanks for the sarcasm, but it's not an assignment. I'm simply trying to learn more about C++. Also, just FYI, I'm still in high school. Haven't even chosen a major.

                        L Offline
                        L Offline
                        led mike
                        wrote on last edited by
                        #11

                        Klazen wrote:

                        but it's not an assignment. I'm simply trying to learn more about C++. Also, just FYI, I'm still in high school.

                        How stupid of me not to know that! :rolleyes:

                        led mike

                        1 Reply Last reply
                        0
                        • G Gregory Bryant

                          start everything out using a char varable char number[100] then us a static_cast to turn it in to a int if the user enters a charcter they going to get sumthing crazy but other then that you could use if or catch statements

                          C Offline
                          C Offline
                          Christian Graus
                          wrote on last edited by
                          #12

                          Please don't answer questions until you know the answers. This is just plain wrong on so many levels.

                          Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups