Populating an array with the string of another array and turning it into an INT

0 Toyen . · September 28, 2015
Hello everyone!

I am having a struggle with writing my program.

I am getting the user input via scanf function that is being stored in a character array and then I want to find out whether the user input a number or not.

If he entered 123 for example, I want to relocate the content of that input array into another array that will store the number, then turn the array with the number into an int and print it out.

So the program looks like this:

- scan the input
- then loop via the for loop starting at i = 0, until i < the length of the input string, i++
- and if input is a digit, set input = digit  (that is how I thought I would relocate the string from one array to another if it is a digit)
- then I used the atoi function to turn the digit array into an int and set it equal to an int variable
- however when I print the int variable, it prints 0 therefore its not working

Any suggestions how to approach this?

Thanks and have a great day : )

Post a Reply


Oldest  Newest  Rating
0 Toyen . · September 28, 2015
Hello there!

Thanks for your reply but I need to use scanf function, would that be doable?
0 Toyen . · October 2, 2015
Hello there, I now understand what you meant and this method is good but it does not really suit my needs. Sorry I should have been a bit more specific.

For example, if the user entered 1543153dasdsadsad via scanf, I want to warn them that mixing numbers and characters is not allowed.

So I guess this function is not really made for this kind of problem?
0 Toyen . · October 3, 2015
Nope, if I enter this: asdsadas4142, then it says not a number, but when the input starts with a number like this: 15142sdasdsa

then it prints the number and cuts the other characters out instead of printing "not a number"
0 c student · October 4, 2015
scan as string into an array and then check each element in the array
0 Toyen . · October 4, 2015
Hey there c student!

That is what I was trying to do.

If the user input started with a digit, I was running a while loop and while the current element of the array the user typed was a digit, I stored it in another array that was later turned into an int so I could do arithmetic stuff with it.

So, if the user typed 1215adsd, my loop takes 1215, stores it into a separate array where I wanna have the digit and turns it into an int and stops.

However I' d like to have a warning pop up if the loop comes across a character that is not a digit saying something like:

"Dont mix digits and characters" but I have no idea how would I go about it.
0 c student · October 4, 2015
as i said, check each element.  if you find that there is a character after some numbers, then print the warning.
0 Toyen . · October 11, 2015
Solved the problem, I decided to cut the rest of the input instead : ) Thanks everyone for feedback : )
0 Linguist Llama · October 15, 2015
What should your program do when it comes across a newline ('\n')?

A newline is typically required before any of your user input will be passed from your OS to your program; the OS typically stores a line of characters up until the '\n' character (corresponding to the enter or return key) is sent, then the entire line is sent to your program.

Your logic is unnecessarily complex.

int value;
if (scanf("%d", &value) != 1 || getchar() != '\n') {
   puts("WARNING: Invalid input detected");

Notice how no array is needed here? Peace! -drops mic-
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator