473,387 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

newb: inputting int-s

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

Nov 13 '05 #1
10 2130
Ema
"ASiF" <so***@localhost.localdomain> ha scritto nel messaggio
news:bj**********@tomahawk.unsw.edu.au...
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[i] but every time you have to
increase i until you reach a space.

Bye,
Ema
Nov 13 '05 #2
>>
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[i] 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!
Nov 13 '05 #3
ASiF wrote:
Hi i am completely novice at programming, would appreciate some help.


Do you mean C is your first language? I wouldn't recommend that.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #4
ASiF wrote:
Hi i am completely novice at programming, would appreciate some help.


Do you mean C is your first language? I wouldn't recommend that.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #5
Tom Zych <tz******@pobox.com> wrote:
ASiF wrote:
Hi i am completely novice at programming, would appreciate some help.


Do you mean C is your first language? I wouldn't recommend that.


Why not?

What are your recommendations?

Irrwahn.

--
6 * 9 = 42 (base 13)
Nov 13 '05 #6
Irrwahn Grausewitz wrote:
Tom Zych <tz******@pobox.com> wrote:
Do you mean C is your first language? I wouldn't recommend that.

Why not? 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.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #7
Tom Zych <tz******@pobox.com> wrote:
ASiF wrote:
Hi i am completely novice at programming, would appreciate some help.


Do you mean C is your first language? I wouldn't recommend that.


Why not?

What are your recommendations?

Irrwahn.

--
6 * 9 = 42 (base 13)
Nov 13 '05 #8
Irrwahn Grausewitz wrote:
Tom Zych <tz******@pobox.com> wrote:
Do you mean C is your first language? I wouldn't recommend that.

Why not? 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.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #9
ASiF wrote:
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[i]);

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[i] = '\0';
return i;
}

Nov 13 '05 #10
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.

--
Stephan
Nov 13 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Travis Ray | last post by:
Hi, I just started a java class and I have this assignment and I'm stumped... I realize the code is messy... but I just started... so go easy on me :) Java Problem I am working on this...
5
by: Alexandre | last post by:
Hi, Im a newb to dev and python... my first sefl assigned mission was to read a pickled file containing a list with DB like data and convert this to MySQL... So i wrote my first module which...
8
by: Kruton | last post by:
What is the easiest way to take a string from a console while including whitespaces? This program's user will input a line of text that will consist of multiple tokens, but the number tokens and...
11
by: The_Kingpin | last post by:
Hi all, I'm new to C programming and looking for some help. I have a homework project to do and could use every tips, advises, code sample and references I can get. Here's what I need to do....
3
by: shdwsclan | last post by:
I am native to various languages but bitwise operators just kill me. I see how much I take object oriented languages for granted. I like all the other c derivitives but ANSI C is making me loose my...
1
by: Seth King | last post by:
I am used to programming in embedded C and I want to input a binary number into an int. usually I would just use bin as a suffix or b as a prefix int x = b001 or int x = 001110bin the hex...
3
by: zylo | last post by:
hello ive just started learning c++ and im really stuck. the program doesnt seem to beable to read the if statement properly # include <iostream> using namespace std; int getDaysHire(int);...
1
by: Ramper | last post by:
Have a .txt document as: String int int int int int int int
1
by: shadowofanubis66 | last post by:
Basically, the project I'm working on this week is to make a school lunch menu with a GUI, when the person types in a day, the menu for that day shows up. We have two files, ones a day off text file...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.