-1

The program is supposed to do the following:

Add Major details

Add University details

Add major to university

Update major cost

Search for a major

Before completing the main function I'm facing a problem:

When I use scanf instead of gets in the functions, It asks 2 questions without letting me to fill the previous one. ex: Enter university's name: Enter university's address: And when I used gets I faced the same problem.

If there's any other mistake please hint me

Thanks in advance!

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

//Define Structures
typedef struct {
    char name[30];
    char department[30];
    int nb_students;
    int credits;
    float cost;
    char adv_name[15];
}Major;
typedef struct {
    char name[50];
    char address[30];
    Major uni_majors[50];
    int nb_majors;
}University;

// Define Functions Prototypes
Major majors_function();
University university_function();
void AddMajor(Major *major,University *university);
University UpdateMajor(char nameMajor[], University U, float newCost);
void SearchMajor(Major major,University university);

int main(int argc, const char * argv[])
{
    Major new_major;
    University new_university;

    new_major = majors_function();
    new_university = university_function();


    return 0;
}

// Fills The Major Details Function
Major majors_function() {

    Major major;

    printf("Enter Major name: ");
    gets(major.name);

    printf("Enter Department name: ");
    gets(major.department);

    printf("Enter number of students: ");
    scanf("%d",&major.nb_students);

    printf("Enter number of credits: ");
    scanf("%d",&major.credits);

    printf("Enter the cost of credit: ");
    scanf("%f",&major.cost);

    printf("Enter the Advisor's Last Name: ");
    scanf("%s",major.adv_name);

    return major;
}

// Fills the university details Function
University university_function() {
    University university;



    printf("Enter university's name: ");
    gets(university.name);

    printf("Enter university's address: ");
    gets(university.address);

    printf("Enter number of majors in this univeristy: ");
    scanf("%d",&university.nb_majors);


    return university;
}

// Adds Major to a university
void AddMajor(Major *major,University *university) {

    university->nb_majors = 0;
    if(university->nb_majors <50) {
        university->uni_majors[university->nb_majors] = *major;
    }
    else
        printf("No Available space");

    university->nb_majors++;


}

// Update Major's Cost
University UpdateMajor(char nameMajor[], University U, float newCost) {

    if(strcmp(nameMajor,U.uni_majors->name)) {
        U.uni_majors->cost = newCost;
    }
    return U;
}

// Searches for a major in a university
void SearchMajor(Major major,University university) {
    if(strcmp(university.uni_majors->name,major.name))
        printf("The total cost of this major is %.2f",(major.cost*major.credits));
    else
        printf("There is no such major!");
}
Ali Zahr
  • 64
  • 3
  • 16
  • Read this about [scanf issues](http://stackoverflow.com/questions/9562218/c-mutliple-scanfs-when-i-enter-in-a-value-for-one-scanf-it-skips-the-second-s) – Suvarna Pattayil Nov 08 '13 at 09:32

2 Answers2

2

When using scanf to scan strings or characters. you might want to skip leading whitespace (like newlines). This can simply be done by asking scanf to skip whitespace, by adding a single space in front of the format code, like

scanf(" %s",major.adv_name);
/*     ^      */
/*     |      */
/* Note space */

You might want to read more about scanf and its siblings on e.g. this reference page.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

NEVER NEVER NEVER NEVER NEVER NEVER NEVER NEVERN NEVER NEVER use gets, not for homework assignments, even for toy code. It was deprecated in the 1999 standard and has been removed from the 2011 standard. It will introduce a major point of failure in your program. Use fgets instead:

fgets( major.name, sizeof major.name, stdin );

and check the return value to make sure it succeeded.

Alternately, you can use scanf, but you'll want to specify the max buffer size in the conversion specifier:

scanf( "%29s", major.name ); // leave 1 space for the 0 terminator

The only problem with this is that the size has to be hardcoded; you can't pass it as an argument like you do with printf. Alternately, you can build the format string at runtime, like:

char fmt[5]; // good for %0s to %99s
sprintf( fmt, "%%%zus", sizeof major.name );
...
scanf( fmt, major.name );

Frankly, you're better off using fgets for everything, including the numeric inputs.

char inbuf[10];
char *chk;

// Read the number of students as text, then convert with the
// strtol library function; allows us to catch and reject
// non-numeric input. 
printf( "Enter number of students: " );
fflush( stdout );
if ( fgets( inbuf, sizeof inbuf, stdin ) != EOF )
{
  int tmp = (int) strtol( inbuf, &chk, 10 );
  if ( isspace( *chk ) || *chk == 0 )
  {
    major.nb_students = tmp;
  }
  else
  {
    fprintf( stderr, "%s is not a valid number\n", inbuf );
  }
}
John Bode
  • 119,563
  • 19
  • 122
  • 198