justsum13lse

Structures homework

4 posts in this topic

Ok, most of this code works.  I am just trying to make an exit procedure if they put in ZZ or zz for the first name.  I have tried a few different ways 

 

#include <stdio.h>
#include <string.h>

struct student{
    
    char f_name[20];
    char l_name[20];
    int id_num;
    float test1;
    float test2;
    float test3;
}s[10];

int main(void)

{

char ch;

float average;

int i;

for(i=0;i<=1;i++)
{
    printf("Enter first name(ZZ to quit): ");
    scanf("%s", &s.f_name);
    ch = s.f_name;
    if(ch== 'zz')
        break;
    else{
    printf("Enter last name: ");
    scanf("%s", &s.l_name);
    printf("Enter student ID number: ");
    scanf("%d", &s.id_num);
    printf("Enter the 3 test scores: ");
    scanf("%f %f %f", &s.test1, &s.test2, &s.test3);
    }
    
}
i--;
printf("Student\t\tStudent ID\tFinal Grade\n");
for(i;i>=0;i--)
{
average = (s.test1 + s.test2 + s.test3) / 3;     
printf("%s, %s\t%d\t\t%.2f\n", s.l_name, s.f_name, s.id_num, average);
}

return 0;

}

 

Everything works properly except the ZZ to quit.  If anyone could throw some help I would appreciate it, as always. 

Thanks

Share this post


Link to post
Share on other sites

Changed my i variable to a to hopefully avoid the italics that are created when I use an array of i.

 

 

#include <stdio.h>
#include <string.h>

struct student{
    
    char f_name[20];
    char l_name[20];
    int id_num;
    float test1;
    float test2;
    float test3;
}s[10];

int main(void)

{

char ch;

float average;

int a;

for(a=0;a<=1;a++)
{
    printf("Enter first name(ZZ to quit): ");
    scanf("%s", &s[a].f_name);
    ch = s[a].f_name;
    if(ch== 'zz')
        break;
    else{
    printf("Enter last name: ");
    scanf("%s", &s[a].l_name);
    printf("Enter student ID number: ");
    scanf("%d", &s[a].id_num);
    printf("Enter the 3 test scores: ");
    scanf("%f %f %f", &s[a].test1, &s[a].test2, &s[a].test3);
    }
    
}
a--;
printf("Student\t\tStudent ID\tFinal Grade\n");
for(a;a>=0;a--)
{
average = (s[a].test1 + s[a].test2 + s[a].test3) / 3;     
printf("%s, %s\t%d\t\t%.2f\n", s[a].l_name, s[a].f_name, s[a].id_num, average);
}

return 0;

}

 

 

 

Share this post


Link to post
Share on other sites

Your code is kind of a mess, although maybe that is mostly due to not using BBcode for code: 

[code][/code]
Spoiler
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student{
    char f_name[20];
    char l_name[20];
    int id_num;
    float test1;
    float test2;
    float test3;
}s[10];
int main(void)
{
    float average;
    for (int a = 0;a <= 1;a++)
    {
        printf("Enter first name(ZZ to quit): ");
        scanf("%s", &s[a].f_name);
        if (strcmp(s[a].f_name, "ZZ") == 0)
        {
            printf("Quitting!\n");
            exit(0);
        }
        else
        {
            printf("Enter last name: ");
            scanf("%s", &s[a].l_name);
            printf("Enter student ID number: ");
            scanf("%d", &s[a].id_num);
            printf("Enter the 3 test scores: ");
            scanf("%f %f %f", &s[a].test1, &s[a].test2, &s[a].test3);
        }
    }
    printf("Student\t\tStudent ID\tFinal Grade\n");
    for (int a = 0;a >= 0;a--) {
        average = (s[a].test1 + s[a].test2 + s[a].test3) / 3;     
        printf("%s, %s\t%d\t\t%.2f\n", s[a].l_name, s[a].f_name, s[a].id_num, average);
    }
    return 0;
}

 

I didn't want to do this for you, but I sorta did so sorry about that. I hide it behind a spoiler tag.

Couple notes:

  • For for() loops, usually you initialize and declare the integer you are going to use in the actual loop itself. IE for (int i = 0; i < 10; i++)
  • char types are for a single char. I took your char ch out because it didn't seem necessary to save s[a].f_name locally (and it was busting up the program.) 'Z' or 'z' could be a char, but not "ZZ."
  • For string comparison, you have to use strcmp() which takes two const char * and returns 0 if they're the same . It returns negative or positive otherwise (see 

    Please login or register to see this link.

    )
  • Depending on what you meant by "quit" you need to exit() or possibly return 0. exit() requires that you include stdlib.h so that's what I did.

Most of your code seemed pretty good though. I'm a C noob so this info may be incomplete or just wrong. I did no testing of your code other than seeing if I could get ZZ to work.

Share this post


Link to post
Share on other sites

That works great.  But I didn't explain well what I was trying to achieve.  I was in the middle of studying and trying to figure it out with someone else in class.

 

The goal of the project was to create a structure that asks for the first and last name, the student ID, and 3 test scores.  The user can enter up to 10 names, or can end it at anytime when the user types in zz or ZZ.  When the user types in zz or ZZ, the program should display everything you have entered up until that point.  So if I had entered 3 names up until the point I entered ZZ, it would display

John Smith      50.0 F

Jerry Garcia    80.0 B

Jane Doe    95.5 A

I didn't know that the strcmp could be used in the fashion that you used it in.  That really helped.  I wasn't clear enough in explaining how my teacher wants the program to work.  I was busy while I was asking for help on this board.  But I almost have the program working correctly.  Just a problem with the scanf that involves 2 strings I mention below.  I also get bonus points if I can make the names display in alphabetical order.  I am working on that.  I tried a

if(strcmp(s[a].l_name, s[a+1].l_name) < 0)

s[a].temp = s[a].l_name;

s[a].l_name = s[a+1].l_name;

s[a+1].l_name = s[a].temp;

haven't quite got it working yet.

 

And I used some of your code, it was kind of what I was looking for.  I used your if(strcmp(s[a].f_name, "ZZ") == 0) to break; else continue.  But now I have a problem.  On the line where it asks for first and last name by doing

scanf("%s %s", &s[a].f_name, &s[a].l_name)

if(strcmp(s[a].f_name, "ZZ") == 0)

break;

else

{

all the other stuff

}

but because of the scanf("%s %s"), it doesn't immediately break when the user hits enter.  It waits for the seconds string to be entered.  Once the second string is input by the user, it breaks and works exactly how I want it to.  But I need it to break immediately when the user enters ZZ.  I know I could do a

scanf("%s", &s[a].f_name);

if(strcmp(s[a].f_name, "ZZ") == 0)

break;

else

scanf("%s", &s[a].l_name);

instead of

scanf("%s %s", &s[a].f_name, &s[a].l_name)

if(strcmp(s[a].f_name, "ZZ") == 0)

break;

but according to the way I believe the teacher wants it, she wants to enter both names on the same line.  

 

This is what I have so far.  This version works exactly the way I want it to, except for the waiting on the second string to be entered in the scanf I mentioned above.  And also, it doesn't arrange the last names in alphabetical order yet.

#include <stdio.h>
	#include <string.h>
	#include <stdlib.h>
	struct student{
	    
	    char f_name[20];
	    char l_name[20];
	    char temp[20];
	    int id_num;
	    float test1;
	    float test2;
	    float test3;
	}s[10];
	int main(void)
	{
	char ch, grade;
	char temp;
	float average;
	int kids;
	int a;
	for(a=0;a<10;a++)
	{
	    printf("Enter first name(ZZ to quit): ");
	    scanf("%s %s", &s[a].f_name, &s[a].l_name);
	    if(strcmp(s[a].f_name, "ZZ") == 0)
	    break;
	    else
	    {
	    printf("Enter student ID number: ");
	    scanf("%d", &s[a].id_num);
	    printf("Enter the 3 test scores: ");
	    scanf("%f %f %f", &s[a].test1, &s[a].test2, &s[a].test3);
	    }
	}          
	a--;
	printf("Student\t\tStudent ID\tFinal Grade\n");
	for(a;a>=0;a--)
	{
	average = (s[a].test1 + s[a].test2 + s[a].test3) / 3;     
	if(average>= 90)
	    grade = 'A';
	else if(average>= 80 && average <90)
	    grade = 'B';
	else if(average>= 70 && average< 80)
	    grade = 'C';
	else if(average >= 60 && average<70)
	    grade = 'D';
	else
	    grade = 'F';
	printf("%s, %s\t%d\t\t%.2f  \t%c\n", s[a].l_name, s[a].f_name, s[a].id_num, average, grade);
	}
	return 0;
	}
	

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now