Thread: visual c++... what did I do wrong?

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    3

    Unhappy visual c++... what did I do wrong?

    I'm teaching myself Visual C++ by way of a textbook and Express Ed. 2005.

    The textbook has me making this lame little number adder (not a full calculator) and it seems simple enough. I was able to write the program with the book's guidance and the program works if you enter numbers and not other strange characters. I have my input variables set to "float" and all is well...

    ... next lesson the book wants you to add code to test your input for whether characters other than numbers have been input into the input boxes (using the TryParse method and if/else phrases... and then a subroutine to tie it all together).

    And I understand what I'm supposed to be doing... I type everything right (I think) and with the new code, the program still runs when only regular numbers are input. But the program crashes when I put in letters... and it should just return 0 to my answer box.

    here's the block of code I wrote:

    Code:
    private: System::Void calcbutton_Click(System::Object^ sender, System::EventArgs^ e) {
    float valueone; //define 3 variables, could use int, but float will let decimal numbers work too
    float valuetwo;
    float answer;
    valueone = InputCheck(inputbox1->Text, valueone);
    valuetwo = InputCheck(inputbox2->Text, valuetwo);
    //test input1
    
    //valueone = float::Parse(inputbox1->Text);//read text to variables... float::Parse() lets data be read from a string to a float
    
    //test input2
    
    // valuetwo = float::Parse(inputbox2->Text);//read text to variables... float::Parse() lets data be read from a string to a float
    
    //process variables
    answer = valueone + valuetwo;// add the variables into the answer variable
    answerbox->Text = answer.ToString();//display answer. answer.ToString() reads variable and prints to a string for display.
    
    }
    private: float InputCheck(System::String^ textValue, float% value)
    {
    if (float::TryParse(textValue, value) == true)
    {
    value = float::Parse(textValue);
    }
    else
    {
    value = 0;
    }
    return value;
    }
    just ignore all my notes... and notice that the original input to Parse lines have been commented out (that was the original code and it works fine...) then I tried adding the InputCheck subroutine and it doesn't work... and I don't know why... Individual If/else statements for each of the variables "valueone" and "valuetwo" don't work either... WHY!? Did I type something wrong that I'm not seeing?

    Build/Debug returns no errors... Only when I run the program through Visual C++ and enter letters for my variables then a window pops up saying roughly this(translated from Japanese):

    An exception not handled by 'System.FormatException' was displayed by mscorlib.dll

    included information: the form of an entered line is incorrect.

    (I'll run the program on my computer at home and get the English version of the error message later)

    I assume that I must have typed something wrong... but what?
    Should the subroutine be nested within the same "calcbutton_Click" method like all the other processes? I tried that though and it didn't solve the problem....

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... that looks like C++/CLI. The MSDN docs says something about Single.TryParse Method (String, Single), namely that: "When this method returns, contains single-precision floating-point number equivalent to the numeric value or symbol contained in s, if the conversion succeeded, or zero if the conversion failed."

    As such, you are probably looking to write:
    Code:
    private:
        System::Void calcbutton_Click(System::Object^ sender, System::EventArgs^ e)
        {
            // let decimal numbers work too
            float valueone = InputCheck(inputbox1->Text, valueone);
            float valuetwo = InputCheck(inputbox2->Text, valuetwo);
    
            //process variables
            float answer = valueone + valuetwo;
            answerbox->Text = answer.ToString();
        }
    private:
        float InputCheck(System::String^ textValue, float% value)
        {
            Single::TryParse(textValue, value);
            return value;
        }
    That said, are you sure that you want to learn C++/CLI rather than standard C++?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    Quote Originally Posted by laserlight View Post
    hmm... that looks like C++/CLI. The MSDN docs says something about Single.TryParse Method (String, Single), namely that: "When this method returns, contains single-precision floating-point number equivalent to the numeric value or symbol contained in s, if the conversion succeeded, or zero if the conversion failed."
    so basically the if/else statement that the book tells me to use is redundant?


    That said, are you sure that you want to learn C++/CLI rather than standard C++?
    Well, I'm already 24 and kind of late into the game. So I needed something I could learn quickly just to have a basic background in programming in general before going on to learn other things. I don't think I want to be an actual programmer, but having a little insight into it wouldn't hurt I figured.

    I'm actually working towards being a network admin.

    That said, a friend actually suggested Visual C++ since i have a tiny bit of experience in C++ from highschool and the software does a lot of the nitty gritty work for you (and seems to be popular enough over here in Japan).

    I don't really know what the difference between C++/CLI and "standard" C++ is... so I can't really answer that question.

    EDIT:
    Not really sure what that "Single" command is in:
    Code:
     Single::TryParse(textValue, value);
    but, I ran the code you suggested and it still didn't work... I get the same error dialogue as before and the Debug pane displays these errors:
    A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
    An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

    Could the problem have anything to do with the fact that I switch between two different language versions of Visual C++ Express on two different computers? The source/solution files are saved on a USB disk...
    Last edited by shimojimatto; 07-24-2007 at 02:28 AM.

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by shimojimatto View Post
    so basically the if/else statement that the book tells me to use is redundant?


    Well, I'm already 24 and kind of late into the game. So I needed something I could learn quickly just to have a basic background in programming in general before going on to learn other things. I don't think I want to be an actual programmer, but having a little insight into it wouldn't hurt I figured.
    I would agree that C++/CLI is not the best language to learn, it's really meant to be a "bridge" between managed and unmanaged code. If you are interested in a managed (.net) language, C# is probably the better route to go. However, since you don't really want to be an actual programmer then maybe you could even take a look at Visual Basic. :-)

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    I guess I will tackle the basics of C# next then. Thanks for the advice!

    now... back to the solution to my current problem?? Even when I input the code that the book gives as an example directly it doesn't work... Any clues?

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    1

    Thumbs up

    I think your problem come from the CultureInfo of your current thread.

    //in c# but it's quite the same in c++/cli.
    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    float.Parse will accept float values formatted like this one : "1.1" (1 dot 1).

    Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
    float.Parse will accept float values formatted like this one : "1,1" (1 comma 1).


    ++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats wrong with visual studio?
    By Masterx in forum Tech Board
    Replies: 10
    Last Post: 06-21-2009, 12:28 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Visual J#
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-08-2001, 02:41 PM
  5. What's wrong with Micorsoft Visual C++?
    By knave in forum C++ Programming
    Replies: 9
    Last Post: 10-04-2001, 08:02 AM