Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String to integer
#21
Quote:We dont know you, you're a nobody, as you never take part in the "qmunity" posts/topics.
The topics you shun.
The non-coding topics.
Where we get to know each other and form bands..
Have fun.

Don't worry, I "have fun". I enjoy QBasic! I don't mind being a nobody on the internet.

Mac
Reply
#22
Mac,

Take heart. IMHO you make positive, informative contributions to this forum, and never look for any opportunity to ridicule others.

*****
Reply
#23
z!re keep this crap in the debate forum. im down... just DONT bring this to the NEWBIE section, chr!st
Reply
#24
I know this is OT, but it may be useful to someone.
In c++, big problems occur if std::cin is fed a character when expecting an integer. As such, I use the following two functions to assign user-input integers to a variable. main() demonstrates how to use the functions.

If anyone has better functions to do the same, or sees problems with mine, I'm interested in hearing your input. The 'large number warning' is in case number wraps...number is still accepted but user is alerted that there may be a problem.

Code:
#include <iostream>
#include <string>

using namespace std;
int gint();  // Get int
unsigned int  gun();  // Get unsigned int

int main(){
  
   cout << "This program demos 2 failsafe \"int getter\" functions.\n"
        << "Enter an Integer ";
   int i = gint();
   cout << "Enter an Unsigned Integer ";
   unsigned int ui = gun();
   cout << "\nI've got 'em...\nyour int is " << i
        << "\nyour unsigned int is: " << ui << "\nover and out!\n";
  
  return 0;
}


int gint(){          // get *int*eger
  string a;
  bool badNum;
  int myint;
  
  int sign;
  do{
     badNum = false;
     getline(cin, a);
     // remove spaces and sign symbol, if present
     while(a[0] == ' ') a.erase(0,1);  
     sign = 1 - (2 * (a[0] == '-'));   // sign depends on a[0]
     if((a[0] == '+') || (a[0] == '-')) a.erase(0,1);  // invalid entry if more than one present
     while(a[a.size()-1] == ' ') a.erase(a.size()-1, 1);  
     while(a[0] == ' ') a.erase(0,1);
    
     int tens = 1;
     myint = 0;
     for(int i = a.size(); --i >= 0; ){  //starts at (a.size() - 1)
        if((a[i] <= '9') && (a[i]  >= '0')){
           myint += (a[i] - '0') * tens;
           tens *= 10;  
        }else{  
           cout << "Input Error.\n"
                << "Please enter a valid INTEGER(leading sign optional): ";
           badNum = true;
           a.erase();  //to prevent large num warning if a.size() > 10
           break;
        }  
     }
     myint *= sign;
     if(a.size() > 10) cout << "Large number warning\n";
  }while(badNum == true);
  return myint;
}


unsigned int gun(){  // get *un*signed int
    string a;
    unsigned int myuint;
    bool badNum;
    do{
        getline(cin, a);
        // remove spaces and sign symbol, if present
        while(a[0] == ' ') a.erase(0,1);  
        while(a[a.size()-1] == ' ') a.erase(a.size()-1, 1);  
        if(a[0] == '+') a.erase(0,1);  // invalid entry if more than one present
        while(a[0] == ' ') a.erase(0,1);
        
        // set initial values
        myuint = 0;
        badNum = false;
        unsigned int tens(1);
        
        
        for(int i = a.size(); --i >= 0; ){
            if((a[i] <= '9') && (a[i]  >= '0')){  // a number
                myuint += ((a[i] - '0') * tens);  // use numeric portion
                tens *= 10;  
            }else{
                cout << "Input Error.\n"
                     << "Please enter a valid UNSIGNED INTEGER(leading sign optional): " << endl;
                badNum = true;
                a.erase();
                break;
            }  
        }
    }while(badNum == true);
    if(a.size() > 10){
        cout << "Large number warning" << endl;
    }
    return myuint;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)