C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 1st, 2012, 08:40 AM
just_bri just_bri is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 just_bri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 8 m 31 sec
Reputation Power: 0
Question 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!

Reply With Quote
  #2  
Old October 1st, 2012, 09:10 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 10 m 17 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old October 1st, 2012, 09:26 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
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;

Reply With Quote
  #4  
Old October 1st, 2012, 09:32 AM
just_bri just_bri is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 just_bri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #5  
Old October 1st, 2012, 09:51 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
Yes, you need arrays (possibly of some structure).

Reply With Quote
  #6  
Old October 1st, 2012, 10:24 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 10 m 17 sec
Reputation Power: 383
Do you actually lose credit for reading ahead in the text book?

Reply With Quote
  #7  
Old October 1st, 2012, 10:26 AM
just_bri just_bri is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 just_bri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #8  
Old October 2nd, 2012, 04:37 AM
WaltP WaltP is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 19 WaltP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How to loop/store/print for multiple input?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap