Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-21-2008, 07:53 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,478
Last Blog:
Joomla! And Incompeten...
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default isdigit

I only want a user to input numbers - does C++ have some sort of try-catch? I came across isdigit(), but I believe that is to check whether a character is a number. I came up with this that works excellent to check if a string contains all numbers

cpp Code:
  1. int main () {
  2.      bool isString = false;
  3.      string x;
  4.  
  5.      cout << "Enter a number: ";
  6.  
  7.      cin >> x;
  8.      for(int i = 0; i < x.length(); i++) {
  9.           if (!isdigit(x[i])) {
  10.               isString = true;
  11.          }
  12.      }
  13.  
  14.      return 0;
  15.  
  16. }

But once its done, its still a string and I cant use it to perform math operations on. Can I cast it to an integer? Is there a better way to accomplish this?
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 01-21-2008, 08:06 PM
upredsun upredsun is offline
Newbie
 
Join Date: Jan 2008
Posts: 9
Rep Power: 0
upredsun is on a distinguished road
Default

you can use sprintf function to cast string to an integer.
__________________
http://www.upredsun.com
**Easily and automatically build tcp-based or udp-based network protocol source code**
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-22-2008, 01:15 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

I would rather use std::stringstream, now when we're playing around with std::strings anyway.
Code:
template <typename Type>
Type Convert(std::string String)
{
        Type Variable;
        std::stringstream SS(String);
        SS >> Variable;
        return Variable;
}
This takes a string and spits it out into whatever type you're specifying. To spit it out as an integer, then you shall do like this:
Code:
std::string String = "1234";
int Number = Convert<int>(String);
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-22-2008, 05:46 AM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 507
Last Blog:
Just over the next hil...
Rep Power: 10
G_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura about
Default

C++ has try catch yes but it isn't used all that much (people prefer C style return values).

C++ Code:
  1. try {
  2.   throw 1;
  3. } catch (int i) {
  4.   cout << i << "\n";
  5. }

You can throw just about any type. Java exceptions are generally just glorified strings, you could just throw a string in C++.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-22-2008, 11:37 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 793
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

C++ Code:
  1. #include <math.h>
  2. using namespace System;
  3.  
  4. //this function can handle unsigned numbers in any radix from 1 to 26.
  5. unsigned __int32 StrToNum(char *Str,unsigned int base)
  6. {
  7.     char *BaseStr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  8.     unsigned __int32 Num = 0,currpow,strsz = 0;
  9.    //get string size
  10.     while(Str[strsz] != '\0')
  11.         ++strsz;
  12.     for(currpow = strsz - 1; currpow >= 0; --currpow)
  13.     {
  14.         if(InStr(BaseStr,Str[strsz - currpow - 1]) != -1)
  15.             Num += (unsigned __int32)(InStr(BaseStr,Str[strsz - currpow - 1]) * pow((double)base,(int)currpow));
  16.         else
  17.             throw gcnew System::ArgumentException("Not a number!");
  18.     }
  19.     return Num;
  20. }
  21.  
  22. signed __int32 InStr(char *Str,char SearchChar)
  23. {
  24.     signed __int32 i = 0;
  25.     while(Str[i] != '\0')
  26.     {
  27.         if(Str[i] == SearchChar)
  28.             return i;
  29.         else
  30.             ++i;
  31.     }
  32.     return -1;
  33. }

Last edited by dargueta; 01-22-2008 at 11:40 PM. Reason: Forgot a function
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -5. The time now is 11:37 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads