newb: inputting int-s

A

ASiF

Hi i am completely novice at programming, would appreciate some help.

The program needs to be able to take a set of up to a 100 numbers (between
1 to 9) "inputted"
at the prompt and then assign those number indvidually to seperate
variable and then pass on to another function which will perform other
operations on the numbers.

char line[200]

printf ("Enter the numbers: "); //numbers would be seperated by space
fgets (gridEmulation, sizeof(gridEmulation), stdin); //gets in the numbers

here i get stuck, i was thinking of using sscanf to turn the input vales
back to individual numbers, but how do i assign the number when i dont
even know how many there are going to be.

TIA

ASiF
 
E

Ema

ASiF said:
Hi i am completely novice at programming, would appreciate some help.

The program needs to be able to take a set of up to a 100 numbers (between
1 to 9) "inputted"
at the prompt and then assign those number indvidually to seperate
variable and then pass on to another function which will perform other
operations on the numbers.

char line[200]

printf ("Enter the numbers: "); //numbers would be seperated by space
fgets (gridEmulation, sizeof(gridEmulation), stdin); //gets in the numbers

here i get stuck, i was thinking of using sscanf to turn the input vales
back to individual numbers, but how do i assign the number when i dont
even know how many there are going to be.

TIA

ASiF

By a loop on gridEmulation:

At every loop you can:

use strtok() and execute sscanf() on every token returned

or

use sscanf() with only one "%d" passing to it
&gridEmulation but every time you have to
increase i until you reach a space.

Bye,
Ema
 
A

ASiF

printf ("Enter the numbers: "); //numbers would be seperated by space
fgets (gridEmulation, sizeof(gridEmulation), stdin); //gets in the numbers

here i get stuck, i was thinking of using sscanf to turn the input vales
back to individual numbers, but how do i assign the number when i dont
even know how many there are going to be.

TIA

ASiF

By a loop on gridEmulation:

At every loop you can:

use strtok() and execute sscanf() on every token returned

or

use sscanf() with only one "%d" passing to it
&gridEmulation but every time you have to
increase i until you reach a space.

Bye,
Ema


Thanks emma, tried it out. Works beautifully!
You are a legend!
 
T

Tom Zych

What are your recommendations?

Something not so close to the bare metal. Something where you can
concentrate on programming concepts, instead of the minutiae of
allocating buffers and passing pointers. Python, perhaps. Maybe
Pascal, broken though it is for any real work.
 
S

Steve Zimmerman

ASiF said:
Hi i am completely novice at programming, would appreciate some help.

The program needs to be able to take a set of up to a 100 numbers (between
1 to 9) "inputted"
at the prompt and then assign those number indvidually to seperate
variable and then pass on to another function which will perform other
operations on the numbers.

char line[200]

printf ("Enter the numbers: "); //numbers would be seperated by space
fgets (gridEmulation, sizeof(gridEmulation), stdin); //gets in the numbers

here i get stuck, i was thinking of using sscanf to turn the input vales
back to individual numbers, but how do i assign the number when i dont
even know how many there are going to be.

TIA

ASiF

Thank you for your post.

I've approximated your desires as best I can; this program compiles,
but it is not precisely what you asked for.

--Steve

#include <stdio.h>

int read_line(char str[], int n);

int main()
{
char digits[100];
int how_many;
int i;

printf("Enter up to one hundred digits\n"
"IMPORTANT: Do not put any spaces between the digits: ");
how_many = read_line(digits, 100); /*
* NOTE: At this point in
* the program, you have an
* array, digits[how_many],
* containing the entered digits.
*/
for (i = 0; i < how_many; i++)
printf("%c ", digits);

printf("\n");

return 0;
}

int read_line(char str[], int n)
{
char ch;
int i = 0;

while ((ch = getchar()) != '\n')
if (i < n)
str[i++] = ch;

str = '\0';
return i;
}
 
S

Stephan Wilms

Hi,
here i get stuck, i was thinking of using sscanf to turn the input vales
back to individual numbers, but how do i assign the number when i dont
even know how many there are going to be.

Using "sscanf()" to convert an unknown number of tokens is rather
tricky to do. "sscanf()" will return wether a conversion was
successful or not, or whether the end of the string was reached, but
it wont tell you where the next number in the string begins. I'm not
saying it cant be done, just that there are simpler ways to do it.
Solving the problem with "sscanf()" would require using the "%n" field
specification.

Generally I would recommend using "strtok()" for this type of problem.
But since it is a rather simple specific situation a simple
char-by-char processing loop seems more appropriate:

char *cp;
int i = 0;

for ( cp = line ; *cp != '\0' ; cp++ )
if ( isdigit(*cp) && !isdigit(*(cp+1)) )
some_list[i++] = *cp - '0';

The above example is without any error checking, though, and will
process some cases of illegal input. The version below will skip any
amount of non digit separator chars, will accept and convert single
digit numbers, and will quit when encountering a number with more then
one digit.

for ( cp = line ; *cp != '\0' ; cp++ )
{
if ( isdigit(*cp) )
{
if ( isdigit(*(cp+1)) )
{
printf( "bzzzt : number with more then 1 digit found\n" );
break;
}
else
some_list[i++] = *cp - '0';
}
}

Add an "else" part to the outer "if" for checking the non digit
separator chars.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,573
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top