CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Jan 2009
    Posts
    2

    Complies but doesn't run

    Hi, I'm completely new to programming. That being said, one of the programs I made for an assignment seems to compile, but when I run it I get an error. I can't figure out why its happening. Could someone point me in the right direction?

    PHP Code:
    #include <iostream>
    #include <string>
    using namespace std;


    int main()
    {
        
    int number;
        
        
    cout<<"Please enter an integer greater than or equal to 1000: ";
        
    string digit;
        
    cin>>number;
        


    if (
    number >= 1000)

        {
            
    string last_three_number digit.substr(digit.length()-3);
            
    string first_three_number digit.substr(0,3);
            
    cout<<"Your integer with commas is " <<first_three_number "," last_three_number <<endl;

        }

    else if (
    number 1000)

        {
            
    cout<<"Please enter a higher integer value"<<endl;
        }

    return 
    0;




  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Complies but doesn't run

    Quote Originally Posted by Rayman452 View Post
    Hi, I'm completely new to programming. That being said, one of the programs I made for an assignment seems to compile, but when I run it I get an error.
    Please give us more details on the error.

    Also, since you're new to programming, you need to realize that just because a program compiles, it doesn't guarantee that the program will do what you intended it to do. You still have the logical part of the whole equation to overcome. If you want a program to add two numbers, and instead you mistakingly coded to subtract them, the program may have been correct in terms of syntax, but it is incorrect in that it didn't perform the job requested.

    Also, you must learn to use your debugger, as this will let you know what each variable is at each step of the program.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Complies but doesn't run

    As to your error, where do you assign "digit" a value? You declare it, but you don't assign it any value. You input a value into number, and then for some reason, you assume that "digit" also has this same value as a string. C++ doesn't work that way -- variables must be assigned a known quantity for them to have something to work with.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 23rd, 2009 at 10:32 PM.

  4. #4
    Join Date
    May 2007
    Posts
    811

    Re: Complies but doesn't run

    But you have to give him props. Usage of stl, correct signature of main and code tags .

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Complies but doesn't run

    Quote Originally Posted by STLDude View Post
    But you have to give him props. Usage of stl, correct signature of main and code tags .
    Yes, RayMan452 has picked a good book or teacher to learn from, unlike most newbies to C++ that post here.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jan 2009
    Location
    IN my computer,
    Posts
    24

    Re: Complies but doesn't run

    haha i know how you feel

    what you need to do is figure out a way to make digit and number equal, right now nothing is being entered,

    I tried
    Code:
        
    string digit;
    int number;
    cin >> digit >> number;
    But that means you have to enter the number 2 times, but if you do, it works.

  7. #7
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Complies but doesn't run

    The best strategy for user input is to get all input as strings then parse from the string whatever you need. Parsing ints from strings is very easy with stringstreams and getting input from a user as a string makes your code robust. It is unlikely to crash even if the input comes from a dose of keyboard slapping.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  8. #8
    Join Date
    Jan 2009
    Posts
    2

    Re: Complies but doesn't run

    I'm taking introduction to programming in my university, using Cay Horstmann's "C++ for Everyone" as my textbook. I'm also using Microsoft's visual studio 2008 as my compiler. I'm trying to write a code to insert a comma in the thousands spot.

    If I enter a value less than 100, it works. If I get rid of my if statements, the code manages to work. However, with the if statement I seem to get a debug error, where "this application has requested the run time to terminate it in an unusual way. Please contact the application's support team for more information"

    How do I set it up so that I only have to input the number once for it to be assigned to the string digit, and the int number? I need it to be assigned to an int number so that the if statements can check it and carry out the function.

    What is parsing from the strings and how do I do it?

  9. #9
    Join Date
    Jan 2009
    Location
    IN my computer,
    Posts
    24

    Re: Complies but doesn't run

    Code:
    #include <iostream>
    #include <string>
    #include <sstream> 
    
    using namespace std;
    
    bool StringToInt(const string &s, int &i)
    {
      istringstream myStream(s);
      if (myStream>>i)
        return true;
      else
        return false;
    }
    
    int result;
    int number;
    string digit;
    
    int main()
    {
        
        cout<<"Please enter an integer greater than or equal to 1000: ";
        cin >> digit;
    
    if (StringToInt(digit, result))
      {
       number = result;
      }
      else
      {
        cout << "The Number you put in is not an iterger" <<endl;
      }
    
    
    if ( number >= 1000 )
    
        {
            string last_three_number = digit.substr(digit.length()-3);
            string first_three_number = digit.substr(0,3);
            cout << "Your integer with commas is " <<first_three_number + "," + last_three_number <<endl;
    
        }
    
    else if (number < 1000)
    
        {
            cout << "Please enter a higher integer value" << endl;
        }
    
    return 0; 
    }
    WHEW that was strange.

    Heres where i found the converter

    http://faq.cprogramming.com/cgi-bin/...wer=1046996179


    FYI You should comment your work more

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Complies but doesn't run

    Quote Originally Posted by H aun View Post
    [CODE]

    FYI You should comment your work more
    Very simple code like that doesn't need to be cluttered with comments.

  11. #11
    Join Date
    Jan 2009
    Location
    IN my computer,
    Posts
    24

    Re: Complies but doesn't run

    i think its a good idea to comment a fair ammont, i comment on every line most of the time. But if you do thats ok....

  12. #12
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Complies but doesn't run

    Quote Originally Posted by H aun
    i think its a good idea to comment a fair ammont, i comment on every line most of the time.
    It is a good idea to use descriptive names for identifiers so that the code is "self-commenting". If you need to comment on almost every line, it either means that you have rather undescriptive names, or your comments are redundant.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  13. #13
    Join Date
    Jan 2009
    Location
    IN my computer,
    Posts
    24

    Re: Complies but doesn't run

    Most of them are (haha) but thats ok. I like to know every thing on the spot with good description. Mainly i do it so that people know what the heck they are looking at when i need help, or for when i go back to backed up files so i know what it does

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Complies but doesn't run

    Quote Originally Posted by H aun View Post
    i think its a good idea to comment a fair ammont, i comment on every line most of the time. But if you do thats ok....
    Then either you write very non-intuitive code or you waste a lot of time. If you use self explanatory variable and function names, and keep your code nicely compartmentalized and use proper indentation and brace matching, there should be very little need to comment almost every line.
    Code:
    int a = 1; //set a to 1
    if( a == 1) // see if a is equal to 1
    {
    ...
    }// end if
    Comments like that are just plain goofy.

    If you write your code properly, you'll know what it does without having to read comments.

  15. #15
    Join Date
    Jan 2009
    Location
    IN my computer,
    Posts
    24

    Re: Complies but doesn't run

    Thanks for settin down the law man. good job, wip those programmers into shape. No useless commenting! it clogs up your code

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured