how to convert string to integer using c language

how to convert string to integer using c language

Page 1 of 1

1 Replies - 7672 Views - Last Post: 10 April 2010 - 02:05 AM Rate Topic: -----

#1 9miles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 09-February 10

how to convert string to integer using c language

Posted 10 April 2010 - 01:45 AM

me and my group mates made a game for a project for our class,.. we made a game text twist using C,.. were almost done but theres just one problem,..

in the game that we mase the player must enter number "1" to ENTER the words,.. and it works that way,..but the problem is,.. when the player ENTER "1" AND enter a character / string after that number it still works,..and we dont want that,.. so i hope someone can help us how to convert string to integer using c language...

tnx

Is This A Good Question/Topic? 0
  • +

Replies To: how to convert string to integer using c language

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: how to convert string to integer using c language

Posted 10 April 2010 - 02:05 AM

It still works, even if you enter a non numeric, because "scanf" (which you are probably using), truncates the non-numeric characters. Example: if I enter 341sk, the result would be 341. A way to get over this, would be to accept the input as an array of characters, and then check if you have a non-numeric in that array. If you have a non-numeric, then print an error message, and prompt the user to enter the number again. For char[] to int, you have atoi
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1