The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
How to loop/store/print for multiple input?
Discuss How to loop/store/print for multiple input? in the C Programming forum on Dev Shed. How to loop/store/print for multiple input? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 1st, 2012, 08:40 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Time spent in forums: 2 h 8 m 31 sec
Reputation Power: 0
|
|
How to loop/store/print for multiple input?
Hello. =)
I'm a newbie to C, and I'm currently taking a class in this language. I was given an assignment where I have to gather multiple lines of data (name, SSN, tuition, etc.), for multiple people.
The end result would look like this:
Name: John Doe
SSN: 111-22-3344
Tuition: 2400
Name: Jane Doe
SSN: 222-33-4455
Tuition: 600
Name: Glen Doe
SSN: 333-44-5566
Tuition: 3000
....
That said, I know I'm going to need a loop. And I have to use a sentinel value because my teacher never said how long it should loop.
However, my textbook only shows loops for averaging out grades. It doesn't show ANYTHING about multiple types (and multiple lines) of input. So now I have two major problems:
1. How do I store and print data for multiple input? I don't want to create 20+ values called "name" (name1, name2, etc), "SSN", and "tuition".
2. Is there a way to enter the sentinel value for any piece of data? I was going to have people enter "-1", on the "Name" line (which is the first prompt they'll see), if they want to quit. But what if they enter a name anyway and then decide to quit on the "SSN" line? Can I use a sentinel value of "-1" for the "SSN" line too?
Please help if you can. Thanks in advance!
|

October 1st, 2012, 09:10 AM
|
 |
Contributing User
|
|
|
|
I can't think of a reason to use a data type other than characters. Hence, a hyphen first character is a sufficient indicator.
Code:
/* char name[256]; I am an array. You already know me! */
#define GROUP struct conglomeration
#define RECORDS 131072
#define DIM(A) (sizeof(A)/(sizeof(*A)))
GROUP {
char
name[256],
ssn[16],
tuition[65536]; /* enough digits to store tuition */
} home_work[RECORDS]; /* array of grouped information */
for (i = 0; i < RECORDS; ++i) {
GROUP*student = home_work+i;
fgets(student->name,DIM(student->name),stdin);
if ('-' == *(student->name)) break;
fgets(student->ssn,DIM(student->ssn),stdin);
if ('-' == student->ssn[0]) break;
fgets(student->tuition,DIM(student->tuition),stdin);
if ('-' == *(student->tuition)) break;
/* verify the ssn is valid */
/* verify the tuition is valid */
}
printf("You just registered %d students\n",i);
/* write a pretty display loop for pretty data here */
__________________
[code] Code tags[/code] are essential for python code!
|

October 1st, 2012, 09:26 AM
|
|
|
Quote: | Originally Posted by b49P23TIvg
Code:
#define GROUP struct conglomeration
|
WTH?! ?!?!?
There's a first time for anything!
I'm not against innovation or imagination, but what is wrong with using "struct conglomeration" where that type is required? What do you gain (not counting a few keystrokes) by using a macro instead?
Also note that the usual way to gain keystrokes is with a typedef (I still prefer the literal "struct conglomeration")
Code:
typedef struct conglomeration group;
|

October 1st, 2012, 09:32 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Time spent in forums: 2 h 8 m 31 sec
Reputation Power: 0
|
|
|
Thank you both for the replies, but this code is a bit...complex. We've never gone over #define or GROUP...and we barely covered fgets.
We're pretty much just using these:
printf
scanf
for
while
do
++a
switch
case
counter = 0
...and other basic commands like that.
Is there a way to store data using these basic commands? Or do I have to use advanced functions like the one you posted? I can't seem to find anything on Google, and I don't think any of my ideas will work.
EDIT: I'm seeing some posts (on Google) that talk about arrays. Could I use one of those to store multiple input, like 10 or more names?
|

October 1st, 2012, 09:51 AM
|
|
|
|
Yes, you need arrays (possibly of some structure).
|

October 1st, 2012, 10:24 AM
|
 |
Contributing User
|
|
|
|
|
Do you actually lose credit for reading ahead in the text book?
|

October 1st, 2012, 10:26 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Time spent in forums: 2 h 8 m 31 sec
Reputation Power: 0
|
|
@b49P23TIvg: No, we don't lose credit if we jump a little bit ahead. Arrays are in Chapter 6 (which we aren't on yet)...but the really complex stuff is probably covered in Chapter 18 or something. He'd definitely mark me down because then it's obvious I got a ton of help.
Here's what I came up with for my array. For now, I'm only working with one piece of input at a time...so I'm just trying to figure out the "multiple names" part.
I'm pretty sure this is wrong -- I even got 2 error messages -- but at least it shows I'm trying:
Error Codes Generated
[Error]Line 19 (the "PrintArray" line): undefined reference to `printArray(char const (*) [31])'
[Error] collect2: ld returned 1 exit status
Code:
#include <stdio.h>
void printArray( char const a[][ 31 ] ); /* function prototype */
//function main begins program execution
int main( void )
{
int i = 0;
char name[i][31];
printf("Please enter a name, or type -1 to end: \n");
scanf("%c", name);
for ( i = 0; name[i][31] != -1; i++) {
printf("Please enter a name, or type -1 to end: \n", name[1]);
}
printArray ( name );
return 0; //indicate that the program ended successfully
} //end function main
|

October 2nd, 2012, 04:37 AM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 19
Time spent in forums: 2 h 26 m 27 sec
Reputation Power: 0
|
|
|
My question is more basic information needed from the first post.
What is the purpose of entering these multiple people?
1) It it to collect data for use later in the program?
2) Is it to create a file of students so the next program can read a file?
3) Is it simply to input multiple people just to learn how to use a loop?
#1 requires arrays.
#2 and 3 do not.
So it really depends on exactly what the assignment is meant to teach you. You need to provide more information.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|