Thread: Accessing other .cpp files

  1. #1
    Registered User motarded's Avatar
    Join Date
    Feb 2006
    Location
    Djibouti, Africa
    Posts
    14

    Accessing other .cpp files

    I'm relatively new to programming and I'm having a little bit of trouble. I'm putting together a calculator program that adds, subtracts, whatever. When the addition function is called, it prompts the user for the number of numbers they'd like to add. It then prompts them for the number.

    I got stuck...if the user inputed anything but a valid double data type, the computer would go into an infinite loop; never again prompting the user for data, since they entered a data type that was not a double.

    Eventually, I ended up coding a program called verify. Basically, it takes a string of characters, analyzes them and if it's a valid double number, converts it to a double. (Valid meaning no '-' signs anywhere but the front, only one decimal, no letters, no spaces, no null characters, and it accepts 0). Needless to say, this has been an incredible pain, having taken me about 10 - 12 hours to code. Everything works now, with the exception of "-letters" which returns 0. That should be fixed here shortly.

    Now, my question is, since I now have calculator.cpp and verify.cpp, is there anyway that I can "link" those two programs together? I'd like to have the user input a string of characters from calculator.cpp, pass it to verify.cpp, which will then run it through the ringer, and if it passes all the conditions, return it as a data type of float. Is there a way to do that? Or do I need to implement this 200+ lines of code into calculator.exe?

    P.S. - If anyone is interested in taking a look at the code, let me know and I will post it.

    Thanks,
    motarded

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I got stuck...if the user inputed anything but a valid double data type, the computer would go into an infinite loop; never again prompting the user for data, since they entered a data type that was not a double.
    My guess is that after the error in reading, you didnt clear the state of the stream before the next run, so on the next run the stream was still in a failed state, thus entering an infinite loop.
    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 motarded's Avatar
    Join Date
    Feb 2006
    Location
    Djibouti, Africa
    Posts
    14
    How would I go about clearing the state of the stream?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    in.clear(), where in is the name of your input stream. Could check out C++ I/O from cppreference.com
    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

  5. #5
    Registered User motarded's Avatar
    Join Date
    Feb 2006
    Location
    Djibouti, Africa
    Posts
    14
    That only applies to strings. Originally, I was just using a float variable. The problem with having the user input a string is that I can't add them to each other. So Verify.cpp takes the string and spits it out as a float variable. Do you know how to pass a value from Calculator.cpp to Verify.cpp and back?
    Last edited by motarded; 02-25-2006 at 03:17 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem with having the user input a string is that I can't add them to each other in the instance that an invalid character is entered. So Verify.cpp takes the string and spits it out as a float variable.
    Ah, but you can use the float/double or whatever type you want for input from the stream. With formatted input (i.e. using operator >>), the validation will be performed as you read from the stream. If the input operation fails, chances are its because the input isnt valid, so you notify the user.
    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

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    float f1;
    while (!(cin >> f1)) {
       cin.clear();           // Sets your failed stream flag back to working
       cin.ignore(100,'\n');  // Empties your input buffer
       cout << "Error, invalid input.\nPlease re-enter: ";
    };
    This question is asked so much I just can't believe there isn't a FAQ entry on it... or is there? Hmmm...

    As for passing variables through source files, you're going to have to look into the extern keyword. Though I can't imagine this verification process should take it's own source file. Also, check this one out.
    Last edited by SlyMaelstrom; 02-25-2006 at 03:30 AM.
    Sent from my iPadŽ

  8. #8
    Registered User motarded's Avatar
    Join Date
    Feb 2006
    Location
    Djibouti, Africa
    Posts
    14
    Maelstrom, I tried that, but if the users inputs a number in the beginning, followed by incorrect characters, then rather than giving an error message, the program just assumes that is the number they meant to type. I want to restrict the input to numbers and only numbers.

    I'll give you an example:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
         float f1;
    
         cout <<  "Enter a Number";
         while (!(cin << float))
         {
              cin.clear();
              cin.ignore(100,'\n');
              cout << "Error, invalid input.\nPlease re-enter: ";
         }
    
         cout << f1 << endl;
    
    return 0;
    }
    Outputs this:

    Enter a number: 32 8
    32
    Press any key to continue...

    My aim is to make it so if the user accidentally enters an invalid character, it won't assume that value. Rather, it will prompt them for another number.
    Last edited by motarded; 02-25-2006 at 03:45 AM.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Mmkay, that sounds good, but I still stand that I don't think it requires it's own source. It's your project, though, so write it as you will.
    Sent from my iPadŽ

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    while (!(cin << float))
    You meant >>, right?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    cin.ignore(100,'\n'); // Empties your input buffer
    100??

    Do you know how to pass a value from Calculator.cpp to Verify.cpp and back?
    If you add Verify.cpp to your project, you can call functions in that file from functions in Calculator.cpp. You have to be familiar with how to structure multi file projects.
    Last edited by 7stud; 02-25-2006 at 01:59 PM.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Code:
    cin.ignore(std::numeric_limits < int >::max(), '\n');
    Not 100, std::numeric_limits<int>::max().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The following code checks for a floating point number and only a floating point number, and it displays the error message even if the user enters a number followed by letters. This is what you should be using:
    Code:
    #include <iostream>
    #include <limits>
    
    using namespace std;
    
    int main()
    {
         double f1;
    
         cout <<  "Enter a Number";
         while (!(cin >> f1) || cin.get() != '\n')
         {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Error, invalid input.\nPlease re-enter: ";
        }
    
        cout << f1 << endl;
    
        return 0;
    }
    Also see this post for a useful option that can be used consistently: http://cboard.cprogramming.com/showpost.php?p=503728
    Last edited by Daved; 02-25-2006 at 03:10 PM.

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663


    Code:
    Enter a Number: 12.5  \n
    Error, invalid input.
    Please re-enter:



    Code:
    Please re-enter: 12.5  \n
    Error, invalid input.
    Please re-enter:



    Code:
    Please re-enter: 12.5  \n
    Error, invalid input.
    Please re-enter:

    #$!$#!@#$@#!!

    <ctrl+alt+delete>
    Last edited by 7stud; 02-25-2006 at 05:56 PM.

  15. #15
    Registered User motarded's Avatar
    Join Date
    Feb 2006
    Location
    Djibouti, Africa
    Posts
    14
    Daved, thanks a lot. That code works well. I understand this part:

    Code:
    while(!(cin >> f1))
    This means that if the input is not a float number, the while loop will continue to loop.

    But what is the literal translation of adding this portion of code to the while loop?
    Code:
    || cin.get() != '\n'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 02-12-2009, 02:39 PM
  2. accessing files over wireless network
    By BobMcGee123 in forum Tech Board
    Replies: 4
    Last Post: 07-29-2006, 02:25 PM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Replies: 4
    Last Post: 06-11-2004, 06:18 PM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM