• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C Programming Help

saahmed

Golden Member
I need to allow the user to enter 2 sets of up to 25 numbers. These would be one-dimensional arrays. What is the best way to go about doing this? Do I actually have to do a scanf for every single element? I dont think I can use a char string for this one because in the description of the homework, examples like inta[], and intb[] are shown.
 
if they are all positive you can use something like -1 as a stop value and put the scanf in a while loop

> I dont think I can use a char string for this one because in the description of the homework, examples like inta[], and intb[] are shown.

you could read in a string then use atoi() on each number within the string.

there is a function to read in a line as string "tokens" you might search your C docs for it
 
Originally posted by: DaveSimmons
if they are all positive you can use something like -1 as a stop value and put the scanf in a while loop

> I dont think I can use a char string for this one because in the description of the homework, examples like inta[], and intb[] are shown.

you could read in a string then use atoi() on each number within the string.

there is a function to read in a line as string "tokens" you might search your C docs for it


Yeh, I have to use -1 as the stop value, so I guess I will use while statement, that would probably be easiest, thanks.
 
Is there something wrong with this code? I thought I was doing it right, but the program just stops after the first input. Is the while...for valid? I'm not sure how else to do that.

EDIT: nevermind, forgot the ampersand. But now it wont end when -1 is entered.
 
The for inside the while is valid C, but it doesn't make any logical sense.

The "while" condition (where you check to see if element isn't -1) is only evaluated at the completion of each pass through the while loop.

Basically, it only checks to see if element != -1 at the alst closing bracket for the while loop. So, what you're doing is doing 25 "scanf"s, thenchecking to see if "element is" -1, and if it isn't, then you're doing 25 more scanfs.

What you want is this:
int i = 0;
while(element!=-1){
printf("Please enter your first set of numbers (enter -1 to end set)\n");
scanf("%d",element);
a[i-1]=element;
i++;
}

So that it checks after each scanf to see if it should continue.

Also, initialize element before you use it.
 
Originally posted by: notfred
The for inside the while is valid C, but it doesn't make any logical sense.

The "while" condition (where you check to see if element isn't -1) is only evaluated at the completion of each pass through the while loop.

Basically, it only checks to see if element != -1 at the alst closing bracket for the while loop. So, what you're doing is doing 25 "scanf"s, thenchecking to see if "element is" -1, and if it isn't, then you're doing 25 more scanfs.

What you want is this:
int i = 0;
while(element!=-1){
printf("Please enter your first set of numbers (enter -1 to end set)\n");
scanf("%d",element);
a[i-1]=element;
i++;
}

So that it checks after each scanf to see if it should continue.

Also, initialize element before you use it.
I know you only pasted most of that code but I can still see 3 problems:
-overwrites the buffer if the user never enters -1
-prompts every number instead of every set of numbers
-underwrites the buffer on the first loop

Just for the op's bug-hunting pleasure ... 🙂
 
Another thing you'll want to think about is the fact that you should never allow more than 25 numbers as input. You don't want to write past the end of your array.

Also, when they enter -1 you are storing it in to the array. You don't want to do this, you want to exit without storing it.
 
Originally posted by: kamper
Write his whole assignment for him why don't you? 😛


Yeah, thanks though Kyteland. I actually just wanted a little help rather than someone writing it out for me, that way I actually learn it. But thanks.
 
Looking at other people's code is a great way to learn. I explained how I would do it and then gave example code. What I did is by no means the only way to go about solving the problem. Hopefully you'll look at what I did and figure out why I decided to do it that way. You may come up with something else that suits your needs better.
 
Originally posted by: Kyteland
Looking at other people's code is a great way to learn. I explained how I would do it and then gave example code. What I did is by no means the only way to go about solving the problem. Hopefully you'll look at what I did and figure out why I decided to do it that way. You may come up with something else that suits your needs better.


Yes, you are right. Your example helped me out though mine is quite a bit different becuase some things had to be changed in order to work out other parts of the program that are required.
 
Back
Top