• 0

C++ Character to Integer


Question

Alright guys, sorry in advance if this has been posted before, but I've got quite the dilemma here.

I have a really bad C++ instructor that we are in the progress of removing from duty (He didn't teach us anything, and expects that we know everything..)

So to make a long story short, I have a coding assignment and I need a bit of help.

I have a program that is taking in command line arguments. The user needs to put in 1 integer value, and 4 file names.

I can get the input, but it's in the form of a character array, and I don't know how to convert a character into the integer that it is supposed to be, (Ex: User inputs a 1. I need to get that 1 instead of the ASCII value for it.)

If anyone could assist me with this little adventure, I'd be greatly indebted to you. Thanks in advance!

Link to comment
https://www.neowin.net/forum/topic/428882-c-character-to-integer/
Share on other sites

9 answers to this question

Recommended Posts

  • 0

^Menge is the man. :)

if you subtract 48 from the character storing the '1', you'll get the decimal value. it's basically just subtracting the ascii number where the ascii character '0' is at 48. because '1' is 49, 49-48=1.

#include <iostream>

using namespace std;

void main()
{

	char ch = '1';
	cout << ch-48;
}

  • 0

wrong. When you are using C++, do it the C++ way, not the C way.

#include <sstream>
template<typename T, typename basechar_t=char> T fromString(const std::basic_string<basechar_t>& str)
// contract: T must have a default c`tor and and operor>>(...) defined.
{
  std::basic_stringstream<basechar_t> ss;
  ss << str;
  T t;
  ss >> t;
  return t;
}

int main()
{
   std::wstring ws(L"40403");
   int value(fromString<int, wchar_t>(ws));
}

  • 0

atoi is included in C++ as part of the 'C backwards compatibility, it's defined in cstdlib, which says enough. C++ also "has" printf() and malloc(), doesn't mean you should use them. Sure, you can stick to the C-way of doing things in C++, but why use C++ in the first place then?

If you want to keep using the C-style, use strtol() rather than atoi(). strtol() has error-checking, whereas atoi() bad input could give undefined output.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I agree with open rights group that the age check companies should be regulated but action does need to be taken imo, they may be called irresponsible parents but the fact is that many don't know how to set up blocks. Why should kids suffer because their parents don't know something? I think the UK has always been a bit like this. We don't say healthcare is your responsibility, we tax everyone and make people register with the NHS. We also don't expect everyone to brush their teeth to maintain dental health, we just put fluoride in their water assuming people aren't brushing enough, because some probably aren't or aren't doing it properly. In general, this way of doing things works.
    • Well that's the weird thing, the UK really isn't a very religious country at all and the same politicians that are backing this (basically all of them) also support things like assisted dying which various religions spoke out against. I don't think this is coming from religion, of course religious groups will support it but I don't think they've started it.
    • No.  The only thing you'll be able to capture with that is via antenna (your local NBC, CBS, etc) or if you had a regular cable box you could capture ClearQAM channels (if they are still a thing...probably not though).  Everything else will have encryption which those capture cards aren't meant for.   So yeah...you'll need Verizon's DVR (sucks if you have to pay for it).
    • I've gotta say Microsoft has lost the plot a long, long, long time ago.
    • How is it be technically implemented, I mean if you wanted to include this let's say on this website, at what point do you interrupt the user flow and say yo! Show me your ID or get the hell out? Please go into extreme technical detail. Is it using a third-party, how much information does neowin receive from that third-party, at what point would that information be abused? We all know it would be abused. So hypothetically what would neowin receive from that third-party company?
  • Recent Achievements

    • One Month Later
      Falcon.ai earned a badge
      One Month Later
    • Week One Done
      Falcon.ai earned a badge
      Week One Done
    • Dedicated
      EYEREX earned a badge
      Dedicated
    • First Post
      Electronic Person earned a badge
      First Post
    • Week One Done
      CyberCeps666 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      628
    2. 2
      ATLien_0
      235
    3. 3
      Xenon
      164
    4. 4
      neufuse
      142
    5. 5
      +FloatingFatMan
      123
  • Tell a friend

    Love Neowin? Tell a friend!