Files and function problem! Help needed

This is a discussion on Files and function problem! Help needed within the C++ Programming forums, part of the General Programming Boards category; Hey all, i hope you can help me, ive just started a new course and im into my second term ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    5

    Files and function problem! Help needed

    Hey all, i hope you can help me, ive just started a new course and im into my second term and the C++ is getting harder and harder (ive never done it before). Please dont critisize my code its a mixture of mine and my lecturers and he loves C so theres a bit of C in it (which i just about understand!) He's adamant C is better but im finding it hard to pick up both so im trying to stick to C++ for the time being.

    Anyway, the question is given at the top of the code, it all compiles and runs but when i enter the function to determine the highest and lowest value they keep giving me the wrong answer.

    Code:
    /*Write a program which reads in names and marks from a file, 
    the program should read names until a sentinel value (e.g. "****") is read.  
    The marks will have a maximum of 100.
    
    Write functions to calculate the average mark and to print the name of the 
    student with the best mark in the class.
    */
    
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    
    #include <iostream>
    
    using namespace std;
    
    char **FirstArray;
    int **MarkArray;
    const int value = 10;
    int i = 0;
    int counter = 0;
    int lowest, k;
    
    int minimum(int *marks, int size);			
    int maximum(int *marks, int size);
    float average(int *marks, int size);
    
    int main()
    {  
       char name[20];
       char markStr[20];
       int	mark;
    
       char *names[20];
       int	marks[20];
    
       FILE *myFile;
       myFile = fopen("Names.txt","r");
    
    	if(!myFile)
    	{
    		cout << "File Not Found"; // simple check to show file has been found
    
    	}
    	
    	fscanf(myFile,"%s",name);	//Reads data from the stream and stores them according to the parameter
    								//format into the locations pointed by the additional arguments.
    
    		   while (strcmp(name,"****") )
    		   {
    				fscanf(myFile,"%d",&mark);
    				//fgets(markStr, 20, myFile); // string with no /0 and a /n at the end
    				//sscanf(markStr,"%d",&mark); // string with a /0 at the end
    
    				printf("%s \n",name);
    				printf("%d \n",mark);
    
    				//put these into arrays
    				names[i] = new char[20];
    				strcpy(names[i],name);
    				marks[i] = mark;
    
    				i++;
    				counter++;
    
    
    				fscanf(myFile,"%s",name);
    		   }
    
    
    		cout <<"\nThe average value of this array is:  "<< average(marks, counter)<<endl;
    		cout << names[minimum(marks, counter)] << " Had the Lowest mark with " << marks[minimum(marks, counter)] <<endl;
    		cout << names[maximum(marks, counter)] << " Had the Highest mark with  " << marks[maximum(marks, counter)] <<endl;
    		
    
    	for(int j=0;j<i;j++)
    	{
    		delete(names[j]);
    	}
    
    
       fclose(myFile);
       
       return 0;
    }
    
    
    float average(int *marks, int size)
    {
    	float sum = 0.0f; // Accumulate total in here
    
    		for(int k = 0; k < size; k++)
    		{
    			sum += marks[k]; // Sum array elements
    		}
    	return sum/size; // Return average
    }
    
    int minimum(int *marks, int size)
    {
    	int lowest= marks[0];	
    
    		for(int k = 0; k < size; k++)
    		{
    			if(lowest > marks[k])
    			lowest = marks[k];
    		}
    		
    return k;
    }
    
    
    int maximum(int *marks, int size)
    {
    	int highest = marks[0];	
    
    		for(int k = 0; k < size; k++)
    		{
    			if(marks[k]>lowest)
    			highest = marks[k];
    		}
    
    return k;
    }
    Then all thats in the .txt file is:

    Code:
    Sion
    60
    Fred
    56
    Bill
    65
    Ted
    45
    Joe
    78
    ****
    Please someone help me before i go insane!! I just dont see the problem because ive used the functions before for another assignment question!!

    Any help is appreciated also as i said im new to this so can you talk simply to me!

  2. #2
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    don't use <string.h>

    use <string>

    EDIT:

    this doesn't solve your problem by the way

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    instead of return k
    return lowest or return highest shoulg be.

    and several questions
    1. why C-strings but not strings?
    2. why stdio.h and not fstream?
    3. why <string.h> and not <cstring>?
    4. are you learning C or C++?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Actually, it looks like the functions are trying to return the index of the lowest/highest mark, right? If so, you will need a second variable in your functions. One to store the lowest value, and one to remember which index had that lowest value. Then return the index. Do this for both minimum and maximum. right now you are returning k, which is just the last index used by the loop (and is actually an invalid index since it is one past the array bounds).

    >> don't use <string.h> use <string>
    While using C++ strings is good advice, in this program <string.h> is correct.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Your minimum and maximum functions always return the size of the array instead of the index of the array cell with the lowest/highest values. You need to keep track of this index value within the functions. Here is a possibility for the minimum function:

    Code:
    int minimum(int *marks, int size)
    {
        int lowest = 0;
    
        for(int k = 1; k < size; k++)
        {
            if(marks[lowest] > marks[k])
                lowest = k;
        }
    		
        return lowest;
    }
    Quote Originally Posted by Daved
    >> don't use <string.h> use <string>
    While using C++ strings is good advice, in this program <string.h> is correct.
    <cstring> (and also <cstdio> instead of <stdio.h>)?
    Last edited by hk_mp5kpdw; 01-11-2007 at 11:27 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> <cstring> (and also <cstdio> instead of <stdio.h>)?
    Of course, but both are valid so it is hardly worth mentioning, especially when the OP already knows there are issues mixing C and C++.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    EDIT: Misread the problem... horribly

    As shown above, the problem's in your minimum() funciton
    Last edited by QuestionC; 01-11-2007 at 12:18 PM.
    Callou collei we'll code the way
    Of prime numbers and pings!

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    Thanks all for your help. I understand there are other issues with this program and if i could, i would do it all in C++!

    Actually, it looks like the functions are trying to return the index of the lowest/highest mark, right? If so, you will need a second variable in your functions. One to store the lowest value, and one to remember which index had that lowest value. Then return the index. Do this for both minimum and maximum. right now you are returning k, which is just the last index used by the loop (and is actually an invalid index since it is one past the array bounds).
    I think you have hit the nail on the head there and yes i would be just sending the last index but how can i send back the index of the lowest? I thought i was establishing the lowest with the for loop?!?

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by sion5


    I think you have hit the nail on the head there and yes i would be just sending the last index but how can i send back the index of the lowest? I thought i was establishing the lowest with the for loop?!?
    no in your loop you just saved the min value, not its index. Your index always goes from 0 to size

    look at the hk_mp5kpdw's solution for idea how to store the correct index
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I think you have hit the nail on the head there and yes i would be just sending the last index but how can i send back the index of the lowest? I thought i was establishing the lowest with the for loop?!?
    Well, suppose I tell you that I have an array of numbers, but that you can't look at the array--it's behind my back. Next, I tell you that the lowest number in the array is 3. Can you point to the position in the array where the number 3 is located? That's what you're asking your program to do.

    If you can store the lowest number in a variable, surely you can store the index position of that number in another variable.

  11. #11
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    Cheers 7stud for your explaination i wish my lecturer tought like that! He overcomplicates problems!!

    As for the answer your right im a complete numpty it was hk_mp5kpdw's solution that was correct and it was such a simple fix too!!!

    Thanks all you would probably think im thick if i told you how long thats stumped me for!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21