Thread: Structures and floating point variables (repost with code tags)

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    3

    Structures and floating point variables (repost with code tags)

    I have a problem with floating point variables with structures. When I try to enter a floating point number, it seems to put it in the firstName string array. I don't know why, the code and output is posted below. Please help if you can, I would greatly appriciate it.

    Output:
    Josh Haynes 0.00
    3.45 Haire 0.00

    Code:
    // char firstName[20] 
    // char lastName[20] 
    // float gpa 
    // int ID 
    void Manual_Input() 
    { 
       int count, loop_counter;  
       printf ("How many students do you want to enter?: "); 
       scanf ("%d", &count); 
       student_array = (students *) malloc (count * sizeof(students)); 
       for (loop_counter = 0; loop_counter < count; loop_counter++) 
       { 
         printf ("Enter first name: "); 
         scanf ("%s", student_array->firstName); 
         printf ("Enter last name: "); 
         scanf ("%s", student_array->lastName); 
         printf ("Enter GPA: ");  
         scanf ("%.2f", student_array->gpa); 
         student_array->ID = loop_counter; 
         student_array++; 
       } 
      student_array -= count; 
      printf ("Number of students are %d.\n", count); 
      for (loop_counter = 0; loop_counter < count; loop_counter++)  
      { 
        printf ("%s", student_array->firstName); 
        printf (" %s ", student_array->lastName);  
        printf ("%.2f\n", student_array->gpa); 
        student_array++; 
      } 
      free (student_array); 
      return; 
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    Code:
    for (loop_counter = 0; loop_counter < count; loop_counter++) 
                                               { 
                                                 printf ("Enter first name: "); 
                                                 scanf ("%s", student_array->firstName); 
                                                 printf ("Enter last name: "); 
                                                 scanf ("%s", student_array->lastName); 
                                                 printf ("Enter GPA: ");  
                                                 scanf ("%2f", student_array->gpa); //I deleted the . and it worked better but I didn't use pointers, so my scanf line wase scanf(" %2f",&student_array[loop_counter].gpa); so i don't know if it will be the same but hopefully that's all
                                                 student_array->ID = loop_counter; 
                                                 student_array++; 
                                               }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf ("%.2f", student_array->gpa);
    should be
    scanf ("%f", &student_array->gpa);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM