Help with a piece of code

This is a discussion on Help with a piece of code within the C++ Programming forums, part of the General Programming Boards category; This code is meant to have input as a string, and output as a float, but it returns strange numbers. ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    25

    Help with a piece of code

    This code is meant to have input as a string, and output as a float, but it returns strange numbers. I guess it is something with the converter function at the beginning. Could someone please tell me what's wrong?
    here is the code (it's modified, but it conveys the problem)
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <conio.h>
    #include <sstream> 
    #include <string>
    #include <stdexcept>
    
    using namespace std;
     class BadConversion : public std::runtime_error {
     public:
       BadConversion(const std::string& s)
         : std::runtime_error(s)
         { }
     };
    
        inline float convertToFloat(const std::string& s)
     {
       std::istringstream i(s);
       float x;
       if (!(i >> x))
         throw BadConversion("convertToFlaot(\"" + s + "\")");
       return x;
    };
    int main() 
    {
    string w;
    string q;
    string x;
    string y;
    string m;
    float wint;
    float qint;
    float n;
    char keyPressw;
    char keyPressq;
    char keyPressx;
    char keyPressy;
    cout<<"Input your first point:(";
    do {
        keyPressw=getch();
        if ( keyPressw != char(13)){
             w+=keyPressw;
             cout<<keyPressw;
             }
             } while ( keyPressw != char(13));
    cout<<",";
    do {
        keyPressq=getch();
        if ( keyPressq != char(13)){
             q+=keyPressq;
             cout<<keyPressq;
             }
             } while ( keyPressq != char(13));
    cout<<")";
    cout<<"Input your second point:(";
    do {
        keyPressx=getch();
        if ( keyPressx != char(13)){
             x+=keyPressx;
             cout<<keyPressx;
             }
             } while ( keyPressx != char(13));
    cout<<",";
    do {
        keyPressy=getch();
        if ( keyPressy != char(13)){
             y+=keyPressy;
             cout<<keyPressy;
             }
             } while ( keyPressy != char(13));
    cout<<")\n";
    wint==convertToFloat(w);
    qint==convertToFloat(q);
    wint-qint==n;
    cout<<qint<<"and"<<wint;
    cin.get();
    }
    Remember, if at first you don't succeed, destroy all evidence that you tried in the first place.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Remember the difference between assignment (=) and equality (==).

    Also remember that when assigning a value, the variable you are updating should be on the left side of the equation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats wrong with this piece of code?
    By psycho88 in forum C Programming
    Replies: 3
    Last Post: 12-03-2005, 11:28 PM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. Help with a little piece of code
    By cdonlan in forum C Programming
    Replies: 5
    Last Post: 11-15-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. What is your favorite piece of code?
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-22-2002, 07:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21