Results 1 to 6 of 6

Thread: string to integer?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Location
    California
    Posts
    25

    string to integer?

    After testing the user input and confirming contains only numbers, how do I convert the string to an integer. Have spent hours attempting to do so, obvoisly without success....

    Or change the code below to accept an integer and test...?
    #include <iostream>
    #include <cstring>
    #include <string>
    #include <cctype>

    using namespace std;

    int main()
    {
    string answer;
    char again;
    do
    {
    cout << "Enter a number or letter ";
    cin >> answer;
    for (int i = 0; i < answer.length(); i++)
    {
    if (!isdigit(answer[i]))
    {
    cout << answer << " is not a number!" << endl;
    break;
    }
    }
    cout << "\nAnswer is " << answer << endl;
    cout << "more> ";
    cin >> again;
    }while(again == 'y');
    return 0;
    }

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    #include <sstream>
    
    using namespace std;
    
    int main() {
    	cout << "Please enter an integer: " << flush;
    
    	int num;
    	cin >> num;
    	cout << endl;
    
    	if(cin.fail()) {
    		cout << "You didn't enter a number!" << endl;
    		return -1;
    	}
    
    	cout << "Final number: " << num << endl;
    
    	return 0;
    }


    Similar to yours but I tweaked it for a more C++-like style

    Quick note - if you enter too large a number it overflows and the internal stream sets the fail bit.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    jim mcnamara
    Guest
    Radix lecti mmm?

    couch potato *** laude

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    One does one's best

    PS: Just as well the server went strange earlier; saved me posting the earlier version involving the original isdigit() loop to verify the string.... *shudder*
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Location
    California
    Posts
    25

    parksie code

    much gratitude!!!!!

    Still need help with a slight tweak....

    If I enter 8r33 it accepts the entry as 8.

    The ultimate would be the entire entry must me numeric....however, what I have now is alot more than what I had.

    If there is a simple tweak to check the entire entry, well that would make my semester!!!!!!!

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main() {
    	cout << "Please enter an integer: " << flush;
    
    	int num;
    	cin >> num;
    	cout << endl;
    
    	// get the rest of the stream
    	string s;
    	getline(cin, s);
    
    	if(!s.empty() || cin.fail()) {
    		cout << "You didn't enter a number!" << endl;
    		return -1;
    	}
    
    	cout << "Final number: " << num << endl;
    
    	return 0;
    }
    The getline in the middle extracts what's left on the input line, since the cin >> num has only extracted the first numeric portion it can. If it goes straight to a newline, then the string will be empty since no extra characters were present.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

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