Thread: I dont know whats going on with my program

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    17

    I dont know whats going on with my program

    hey guys im supposed to do the program attached but it does not run how it is supposed to. Can i have some help in fixing this please.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    
    struct StudentData
    {
        char name[25];
        double ID;
        int ELA, Math, Science;
    };
        
    struct StudentData Students[5];     //array to hold 5 StudentData structures
    int reccount = 0;                //keeps count of number of records read in from file
    
    
    void initStructs();
    void printStudentData();    
    
    
    int main()
    {    
        initStructs();
        
        printStudentData();
    }
    
    
    
    
    
    
    void initStructs(){
        
        FILE * input;    //create file pointer
        int pos,ELA,Math,Science = 0;    //struct array pointer
        char n[25];        //to retrieve student name
        double ID;        //to retrieve student ID
        
        
        input = fopen("sdata.txt", "r");
        
        if(!input)
        {
            printf("Unable to open file\n"); //output if file could not be opened
        }
        
        while (fscanf(input,"%s %ID %ELA %Math %Science %lf",&n, &ID,&ELA,&Math,&Science) != EOF) 
        {
            //Read the data from each line into struct
            strcpy(Students[pos].name, n);
            Students[pos].ID = ID;
            Students [pos].ELA = ELA;
            Students [pos].Math=Math;
            Students [pos].Science=Science;
            
        
            reccount++;
            
            
            pos++;
        }
        
        printf("Finished reading from file...\n\n");
        
        fclose(input);    //close the file
        
    }//end initStructs
    
    
    
    
    /* printStudentData is a function that loops through the Students array 
     * prints the data in each student struct to screen 
     */
    void printStudentData(){
        printf("Printing %d Records from file...\n\n", reccount);
        printf("Name\t\t  ID\t\t ELA\t\t Math\t\t Science\n");
        
        for(int i=0; i<reccount; i++){
            printf("%s \t\t\t %8.2lf\n", Students[i].name, Students[i].ID,Students[i].ELA,Students[i].Math,Students[i].Science);
        }
        printf("\n\nFinished printing records.\nPress a key to continue.\n");
        getch();
    }
    Attached Images Attached Images

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to study how printf and scanf work.
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘initStructs’:
    foo.c:47:25: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[25]’ [-Wformat=]
         while (fscanf(input,"%s %ID %ELA %Math %Science %lf",&n, &ID,&ELA,&Math,&Science) != EOF)
                             ^
    foo.c:47:25: warning: unknown conversion type character 0x20 in format [-Wformat=]
    foo.c:47:25: warning: format ‘%E’ expects argument of type ‘float *’, but argument 4 has type ‘double *’ [-Wformat=]
    foo.c:47:25: warning: unknown conversion type character ‘M’ in format [-Wformat=]
    foo.c:47:25: warning: format ‘%lf’ expects argument of type ‘double *’, but argument 6 has type ‘int *’ [-Wformat=]
    foo.c:47:25: warning: too many arguments for format [-Wformat-extra-args]
    foo.c: In function ‘printStudentData’:
    foo.c:80:16: warning: too many arguments for format [-Wformat-extra-args]
             printf("%s \t\t\t %8.2lf\n", Students[i].name, Students[i].ID,Students[i].ELA,Students[i].Math,Students[i].Science);
                    ^
    %ELA and %Math are the names of your variables, not conversions like %d, %f, %s.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    Hi davic01!

    Here an example:
    first student.h:
    Code:
    #ifndef STUDENT_INC_
    #define STUDENT_INC_
    
    #include <iostream>
    #include <iomanip>
    #include <stdlib.h>
    #include <string.h>
    #include <didi.h>
    
    // Setze Vordergrundfarbe \x1b ist ESC 27
    #define RED     "\x1b[31m"
    #define GREEN   "\x1b[32m"
    #define YELLOW  "\x1b[33m"
    #define BLUE    "\x1b[34m"
    #define MAGENTA "\x1b[35m"
    #define CYAN    "\x1b[36m"
    
    #define WHITE_GREEN "\x1b[1;32;47m"  // Backround = White, Foreground = Green  --> Vordergrund;Hintergrund
    #define BLUE_YELLOW "\x1b[1;34;43m"  
    #define WHITE_BLACK "\x1b[1;37;40m"  
    #define BLACK_YELLOW "\x1b[1;30;43m"  
    #define BLACK_WHITE "\x1b[1;30;47m"
    #define BLUE_WHITE "\x1b[1;34;47m"
    #define BLUE_GREEN "\x1b[1;34;42m"
    #define BLACK_GREEN "\x1b[1;30;42m"
    
    #define COLOR_RESET   "\x1b[0m"
     
     
    int initStructs(char filename[], struct StudentData (*Students)[5]);
    void printStudentData(struct StudentData (*Students)[5], int reccount);    
     
    
    #endif
    now student.cpp
    Code:
    #include "student.h"
    
    int initStructs(char filename[], struct StudentData (*Students)[5])
    {
         
        FILE * input;    //create file pointer
        int pos,ELA,Math,Science = 0;  // struct array pointer
        char n[25] = {0};              // to retrieve student name
        double ID;                     // to retrieve student ID
        int rewer = -1;    
     
        input = fopen(filename, "r");
         
        if(!input)
           {
            std::cout << "Unable to open file" << std::endl; //output if file could not be opened
            return rewer;
           }
             
        pos = 0; 
         
        while (fscanf(input,"%lf %s %d %d %d", &ID, n, &ELA, &Math, &Science) != EOF) 
        {
           //Read the data from each line into struct
            strcpy(Students[pos]->name, n);
            Students[pos]->ID = ID;
            Students[pos]->ELA = ELA;
            Students[pos]->Math = Math;
            Students[pos]->Science = Science;
             
             //printf("pos=%d %lf %s   %d   %d  %d\n",pos, ID, n, ELA, Math, Science);
             //printf("pos=%d %lf %s   %d   %d  %d\n",pos, Students[pos]->ID, Students[pos]->name, Students[pos]->ELA, Students[pos]->Math, Students[pos]->Science);
                   
            pos++;
        }
         
       //for(int i=0; i< pos; i++)  printf("nr=%d %8.2lf\t %-20s\t%d\t%d\t%d\n", i, Students[i]->ID, Students[i]->name, Students[i]->ELA,Students[i]->Math,Students[i]->Science);
         
        std::cout << "Finished reading from file:" << std::endl << filename << std::endl;
        std::cout << "reccount: " << pos << std::endl; 
         
        fclose(input);    //close the file
        
     
    return pos;    
    }//end initStructs
     
    
    /* printStudentData is a function that loops through the Students array 
     * prints the data in each student struct to screen 
     */
    void printStudentData(struct StudentData (*Students)[5], int reccount)
    {
        std::cout << "\n\nPrinting " << reccount << " Records from file..." << std::endl << std::endl;
        std::cout << " ID\t\t Name\t\t\tELA\tMath\tScience" << std::endl;
        std::cout << MAGENTA << "-----------------------------------------------------------------" << COLOR_RESET<< std::endl; 
        for(int i=0; i< reccount; i++)
        {
         std::cout << BLUE << std::left << std::setw(10) << Students[i]->ID;
         std::cout << WHITE_BLACK << "\t" << std::left << std::setw(20) << Students[i]->name << "\t";
         std::cout << CYAN << Students[i]->ELA << "\t" << WHITE_BLACK << Students[i]->Math << BLUE_WHITE << "\t" << Students[i]->Science << COLOR_RESET << std::endl;
        }
        std::cout << RED << "-----------------------------------------------------------------" << COLOR_RESET << std::endl; 
        std::cout << std::endl<< "Ende der Liste" << std::endl;
    }
    main.cpp:
    Code:
    #include "student.h"
      
      
     
    int main(int argc, char **argv)
    {    
     struct StudentData Students[5];     //array to hold 5 StudentData structures
     char finam[200] = {0};
     int reccount = 0;                //keeps count of number of records read in from file
       
      strcpy(finam, "test.txt");
     
      reccount = initStructs(finam, &Students);
      printStudentData(&Students, reccount);
      
    return EXIT_SUCCESS;
    }
    here test.txt
    Code:
    1001 Janet  81 42 65
    1002 John   47 68 72
    1003 Jacob  92 94 87 
    1004 Jill   96 88 89
    1005 Jack   54 62 41
    example output:
    I dont know whats going on with my program-studentdata-jpg

    May be some guys think the colours are awful, so change it!

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    rusyoldguy: To be a good programmer you need to learn not to add random or unneeded includes to header files!
    And, using magic numbers is a sign of a newbie programmer.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Code:
    while (fscanf(input,"%lf %s %d %d %d", &ID, n, &ELA, &Math, &Science) != EOF) 
                             ^ Possible buffer overflow error waiting to happen.
    Also not checking the bounds of the Student array when populating is another possible buffer overflow.
    Last edited by jimblumberg; 07-28-2020 at 12:17 PM.

  6. #6
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    Sorry forgot didi.h:
    Code:
    #ifndef LI_H_
    #define LI_H_ 2
    
    extern struct StudentData
    {
        char name[25];
        double ID;
        int ELA;
        int Math;
        int Science;
    }Students;
    
    #endif
    Unneeded includes:

    If somebody want to work with this example, they can delete it themself.
    Otherwise it would be like someone wants to build a house and he don't know how to work with an screwdriver or a saw, hammer or a shovel.


    I think to split code in more files, like headers makes it easier to work with it.

    The direct use of numbers in the code shows here better how does it works.

    Important here is by the function

    "printStudentData"

    You need to take call by reference to get the right values of the struct.
    When you take call by value, you will get nonsens on screen.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    davic01: is this supposed to be a C program or a C++ program?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please check my code, dont know whats wrong
    By Limitless in forum C Programming
    Replies: 9
    Last Post: 11-03-2017, 07:04 AM
  2. Replies: 13
    Last Post: 11-03-2010, 12:45 PM
  3. dont know whats wrong
    By otchster in forum C Programming
    Replies: 2
    Last Post: 11-02-2005, 11:14 AM
  4. whats this?you DONT need a resource editor!
    By DarkViper in forum Windows Programming
    Replies: 7
    Last Post: 11-21-2002, 02:49 PM
  5. join and dont post - whats the point
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-28-2002, 06:39 AM

Tags for this Thread