c++ program

  • venetian
  • Born
  • Born
  • venetian
  • Posts: 4
  • Loc: Australia

Post 3+ Months Ago

Hi I'm in need of a little assistance with my program

i'm using Borland C++ 5.0 on WinXp
-----------------------------------------------


Description:

These marks have been entered on a file – “highmark.dat” The marks are stored on the file as integer values one value per line.

The administrative staff occasionally make mistakes entering the data. Because of this, your program should ignore any marks that are greater than 100 or less than 0.

Your program is to:

1. read the marks into an array;

2. sort the array;

3. produce a report to the screen from the sorted array.

The report is to show:

- the highest mark achieved;

- the lowest mark achieved;

- a graph showing the frequency of each mark in the array.


A sort function is provided (intsort.cpp and intsort.h) and is to be incorporated in a project with your source code.


-------------------------------------------------
data file
========
highmark.dat

101
-1
100
95
0
95
85
95
90
90
90
90
-5
101
85



Example Output:


Report on High Marks

Highest: 100
Lowest: 0

0 *
85 **
90 ****
95 ***
100 *


Code: [ Select ]
#include <fstream.h>
#include <conio.h>
#include <iostream.h>
#include <ctype.h>
#include <iomanip.h>
#include "intsort.h"

//Prototypes
int initial (int Marks[]);
int GetMarks (ifstream &MarkFile, int Marks[]);
int Report (int Marks[]);

//Constant
const int Max_Size=20;

//Global Variables
int AMark, NumMarks, var;
int Count=0;
int HiMark=0;
int LoMark=0;
int Marks[Max_Size];

//***************************************************/
void main()
{
    ifstream MarkFile ("highmark.dat");  //File Details

  clrscr();
    initial (Marks);             //Initializing Variable
    if (MarkFile)              //If File exists Continue
     {
       NumMarks = GetMarks (MarkFile, Marks); //Reads in Marks, returns total number of marks
       SortArray (NumMarks, Marks);      //Sorts Array (Teachers File)
       Report (Marks);             //Displays Report
       MarkFile.close();            //Closes File
       getch();
     }
    else
     {
      cout<<"File Not Found";      //No File Error Message
      getch();
     }
} // end Main

//******************************************************************************

int initial(int Marks[])
{
  while (var!=Max_Size)
  {
   Marks[var]= -1;   //Setting all parts of array to -1
   var++;       //Adding to var
  }
}
//end initial

//******************************************************************************

int GetMarks (ifstream &MarkFile, int Marks[])
{
 MarkFile>>AMark;             //Reads in intial Mark
 while (!MarkFile.eof()&&Count<Max_Size) //While not End of File & Count <  Max Size
    {
     Marks[Count]=AMark;       //Marks array gets Mark
     Count++;             //Adds 1 to Count
     MarkFile>>AMark;         //Reads in next Mark
    }
 return Count;              //Returns Count for NumMarks
} //end GetMarks


//******************************************************************************
int Report (int Marks[])
{
 for (Count=0; Count != Max_Size; Count++)    //For loop to Calculate  Highest & Lowest Marks
 {
    if (Marks[Count]<=100 && Marks[Count]>=0) //Validation
     if (Marks[Count]>HiMark)         //If current array position >  Highest so far
       HiMark = Marks[Count];        //Gets Assigned
     else if (Marks[Count]<LoMark)      //If current array position <  Lowest so far
          LoMark = Marks[Count];     //Gets Assigned
 }
 cout<<"\n \n Final Report"<<endl;            //Displaying  Results
 cout<<"\n \n Highest Mark:"<<(setw(7))<<HiMark<<endl;
 cout<<"\n Lowest Mark:"<<(setw(7))<<LoMark<<endl<<endl;

 for (Count=0; Count != Max_Size; Count++)    //For loop calculates   amount of times a number appears
 {
   if (Marks[Count]<=100 && Marks[Count]>=0)   //Validation
   if (Marks[Count] == Marks[Count+1])     //If Current Position =   Next Position
     cout<<"*";                //Display *
   if (Marks[Count]<=100 && Marks[Count]>=0)   //Validation
   if (Marks[Count] != Marks[Count+1])     //If Current Position !=   Next Position
     cout<<"*"<<Marks[Count]<<endl;      //Display *, Displays Number,   Ends Line
 }
} //end Report
  1. #include <fstream.h>
  2. #include <conio.h>
  3. #include <iostream.h>
  4. #include <ctype.h>
  5. #include <iomanip.h>
  6. #include "intsort.h"
  7. //Prototypes
  8. int initial (int Marks[]);
  9. int GetMarks (ifstream &MarkFile, int Marks[]);
  10. int Report (int Marks[]);
  11. //Constant
  12. const int Max_Size=20;
  13. //Global Variables
  14. int AMark, NumMarks, var;
  15. int Count=0;
  16. int HiMark=0;
  17. int LoMark=0;
  18. int Marks[Max_Size];
  19. //***************************************************/
  20. void main()
  21. {
  22.     ifstream MarkFile ("highmark.dat");  //File Details
  23.   clrscr();
  24.     initial (Marks);             //Initializing Variable
  25.     if (MarkFile)              //If File exists Continue
  26.      {
  27.        NumMarks = GetMarks (MarkFile, Marks); //Reads in Marks, returns total number of marks
  28.        SortArray (NumMarks, Marks);      //Sorts Array (Teachers File)
  29.        Report (Marks);             //Displays Report
  30.        MarkFile.close();            //Closes File
  31.        getch();
  32.      }
  33.     else
  34.      {
  35.       cout<<"File Not Found";      //No File Error Message
  36.       getch();
  37.      }
  38. } // end Main
  39. //******************************************************************************
  40. int initial(int Marks[])
  41. {
  42.   while (var!=Max_Size)
  43.   {
  44.    Marks[var]= -1;   //Setting all parts of array to -1
  45.    var++;       //Adding to var
  46.   }
  47. }
  48. //end initial
  49. //******************************************************************************
  50. int GetMarks (ifstream &MarkFile, int Marks[])
  51. {
  52.  MarkFile>>AMark;             //Reads in intial Mark
  53.  while (!MarkFile.eof()&&Count<Max_Size) //While not End of File & Count <  Max Size
  54.     {
  55.      Marks[Count]=AMark;       //Marks array gets Mark
  56.      Count++;             //Adds 1 to Count
  57.      MarkFile>>AMark;         //Reads in next Mark
  58.     }
  59.  return Count;              //Returns Count for NumMarks
  60. } //end GetMarks
  61. //******************************************************************************
  62. int Report (int Marks[])
  63. {
  64.  for (Count=0; Count != Max_Size; Count++)    //For loop to Calculate  Highest & Lowest Marks
  65.  {
  66.     if (Marks[Count]<=100 && Marks[Count]>=0) //Validation
  67.      if (Marks[Count]>HiMark)         //If current array position >  Highest so far
  68.        HiMark = Marks[Count];        //Gets Assigned
  69.      else if (Marks[Count]<LoMark)      //If current array position <  Lowest so far
  70.           LoMark = Marks[Count];     //Gets Assigned
  71.  }
  72.  cout<<"\n \n Final Report"<<endl;            //Displaying  Results
  73.  cout<<"\n \n Highest Mark:"<<(setw(7))<<HiMark<<endl;
  74.  cout<<"\n Lowest Mark:"<<(setw(7))<<LoMark<<endl<<endl;
  75.  for (Count=0; Count != Max_Size; Count++)    //For loop calculates   amount of times a number appears
  76.  {
  77.    if (Marks[Count]<=100 && Marks[Count]>=0)   //Validation
  78.    if (Marks[Count] == Marks[Count+1])     //If Current Position =   Next Position
  79.      cout<<"*";                //Display *
  80.    if (Marks[Count]<=100 && Marks[Count]>=0)   //Validation
  81.    if (Marks[Count] != Marks[Count+1])     //If Current Position !=   Next Position
  82.      cout<<"*"<<Marks[Count]<<endl;      //Display *, Displays Number,   Ends Line
  83.  }
  84. } //end Report


now the problem i'm getting is this

Info :Compiling E:\PROGRAMMING2\assign1.cpp
Warn :assign1.cpp(56,2):Function should return a value
Warn :assign1.cpp(97,2):Function should return a value
Info :Linking E:\PROGRAMMING2\ASSIGN1.exe
Error:Error: Unresolved external 'SortArray(int,int*)' referenced from module assign1.cpp

I don't understand why the Functions are not returning a value.
Can anyone assist me, I'm very confused :(


thanks

Venetian.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Um for functions that don't return values aren't they declared as
function void functionName (assume this would happen in the prototype to?)

Or you could just have the function return 0;

If Count is not being returned it is probably because it has been declared as a global.

Then again what do I know about c++ only started learning it a week ago
  • dr_geek
  • Beginner
  • Beginner
  • User avatar
  • Posts: 62
  • Loc: london town

Post 3+ Months Ago

Rabid Dog wrote:
Um for functions that don't return values aren't they declared as
function void functionName

No, you declare a function that doesn't return a value just as main() is declared.
So line 47 should be:

void initial(int Marks[])

and line 73 should be:

void Report (int Marks[])


Rabid Dog wrote:
Or you could just have the function return 0;


You never return a value if it doesn't mean anything. That's what void is for.

Also this is more C style than C++. Where are the classes/objects? Where is the STL?
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Like I said only been working in it for a week (not even) and my ASP and PHP (stuff I use everyday) kicked in when saying "function")

So i don't think it was to bad that I picked up the fact that it hadn't been told it was void.

Returning 0 could indicate that the function exited successfully :wink:

I think this is study material to be honest.
  • venetian
  • Born
  • Born
  • venetian
  • Posts: 4
  • Loc: Australia

Post 3+ Months Ago

thanks guys!!!

that fixed the sort error i was getting...
the only problem is that i get this now

Error:assign1.cpp(49,2):Type mismatch in redeclaration of 'initial(int *)'
Warn :assign1.cpp(55,2):Function should return a value
Error:assign1.cpp(75,2):Type mismatch in redeclaration of 'Report(int *)'
Warn :assign1.cpp(97,2):Function should return a value

i changed this
Code: [ Select ]
//Prototypes
int initial (int Marks[]);
int GetMarks (ifstream &MarkFile, int Marks[]);
int Report (int Marks[]);
  1. //Prototypes
  2. int initial (int Marks[]);
  3. int GetMarks (ifstream &MarkFile, int Marks[]);
  4. int Report (int Marks[]);


to this:

Code: [ Select ]
//Prototypes
void initial (int Marks[]);
int GetMarks (ifstream &MarkFile, int Marks[]);
void Report (int Marks[]);
  1. //Prototypes
  2. void initial (int Marks[]);
  3. int GetMarks (ifstream &MarkFile, int Marks[]);
  4. void Report (int Marks[]);


that got rid of the errors i was having above but then this error
returned

Error:Error: Unresolved external 'SortArray(int,int*)' referenced from module assign1.cpp

any ideas?
  • venetian
  • Born
  • Born
  • venetian
  • Posts: 4
  • Loc: Australia

Post 3+ Months Ago

This is my updated code

Code: [ Select ]
#include <fstream.h>
#include <conio.h>
#include <iostream.h>
#include <ctype.h>
#include <iomanip.h>
#include "intsort.h"

//Prototypes
void initial (int Marks[]);
int GetMarks (ifstream &MarkFile, int Marks[]);
void Report (int Marks[]);

//Constant
const int Max_Size=20;

//Global Variables
int AMark, NumMarks, var;
int Count=0;
int HiMark=0;
int LoMark=0;
int Marks[Max_Size];

//***************************************************/
void main()
{
    ifstream MarkFile ("highmark.dat");  //File Details

  clrscr();
    initial (Marks);             //Initializing Variable
    if (MarkFile)              //If File exists Continue
     {
       NumMarks = GetMarks (MarkFile, Marks); //Reads in Marks, returns total number of marks
       SortArray (NumMarks, Marks);      //Sorts Array (Teachers File)
       Report (Marks);             //Displays Report
       MarkFile.close();            //Closes File
       getch();
     }
    else
     {
      cout<<"File Not Found";      //No File Error Message
      getch();
     }
} // end Main

//******************************************************************************

void initial(int Marks[])
{
  while (var!=Max_Size)
  {
   Marks[var]= -1;   //Setting all parts of array to -1
   var++;       //Adding to var
  }
}
//end initial

//******************************************************************************

int GetMarks (ifstream &MarkFile, int Marks[])
{
 MarkFile>>AMark;             //Reads in intial Mark
 while (!MarkFile.eof()&&Count<Max_Size) //While not End of File & Count <  Max Size
    {
     Marks[Count]=AMark;       //Marks array gets Mark
     Count++;             //Adds 1 to Count
     MarkFile>>AMark;         //Reads in next Mark
    }
 return Count;              //Returns Count for NumMarks
} //end GetMarks


//******************************************************************************
void Report (int Marks[])
{
 for (Count=0; Count != Max_Size; Count++)    //For loop to Calculate  Highest & Lowest Marks
 {
    if (Marks[Count]<=100 && Marks[Count]>=0) //Validation
     if (Marks[Count]>HiMark)         //If current array position >  Highest so far
       HiMark = Marks[Count];        //Gets Assigned
     else if (Marks[Count]<LoMark)      //If current array position <  Lowest so far
          LoMark = Marks[Count];     //Gets Assigned
 }
 cout<<"\n \n Final Report"<<endl;            //Displaying  Results
 cout<<"\n \n Highest Mark:"<<(setw(7))<<HiMark<<endl;
 cout<<"\n Lowest Mark:"<<(setw(7))<<LoMark<<endl<<endl;

 for (Count=0; Count != Max_Size; Count++)    //For loop calculates   amount of times a number appears
 {
   if (Marks[Count]<=100 && Marks[Count]>=0)   //Validation
   if (Marks[Count] == Marks[Count+1])     //If Current Position =   Next Position
     cout<<"*";                //Display *
   if (Marks[Count]<=100 && Marks[Count]>=0)   //Validation
   if (Marks[Count] != Marks[Count+1])     //If Current Position !=   Next Position
     cout<<"*"<<Marks[Count]<<endl;      //Display *, Displays Number,   Ends Line
 }
} //end Report
  1. #include <fstream.h>
  2. #include <conio.h>
  3. #include <iostream.h>
  4. #include <ctype.h>
  5. #include <iomanip.h>
  6. #include "intsort.h"
  7. //Prototypes
  8. void initial (int Marks[]);
  9. int GetMarks (ifstream &MarkFile, int Marks[]);
  10. void Report (int Marks[]);
  11. //Constant
  12. const int Max_Size=20;
  13. //Global Variables
  14. int AMark, NumMarks, var;
  15. int Count=0;
  16. int HiMark=0;
  17. int LoMark=0;
  18. int Marks[Max_Size];
  19. //***************************************************/
  20. void main()
  21. {
  22.     ifstream MarkFile ("highmark.dat");  //File Details
  23.   clrscr();
  24.     initial (Marks);             //Initializing Variable
  25.     if (MarkFile)              //If File exists Continue
  26.      {
  27.        NumMarks = GetMarks (MarkFile, Marks); //Reads in Marks, returns total number of marks
  28.        SortArray (NumMarks, Marks);      //Sorts Array (Teachers File)
  29.        Report (Marks);             //Displays Report
  30.        MarkFile.close();            //Closes File
  31.        getch();
  32.      }
  33.     else
  34.      {
  35.       cout<<"File Not Found";      //No File Error Message
  36.       getch();
  37.      }
  38. } // end Main
  39. //******************************************************************************
  40. void initial(int Marks[])
  41. {
  42.   while (var!=Max_Size)
  43.   {
  44.    Marks[var]= -1;   //Setting all parts of array to -1
  45.    var++;       //Adding to var
  46.   }
  47. }
  48. //end initial
  49. //******************************************************************************
  50. int GetMarks (ifstream &MarkFile, int Marks[])
  51. {
  52.  MarkFile>>AMark;             //Reads in intial Mark
  53.  while (!MarkFile.eof()&&Count<Max_Size) //While not End of File & Count <  Max Size
  54.     {
  55.      Marks[Count]=AMark;       //Marks array gets Mark
  56.      Count++;             //Adds 1 to Count
  57.      MarkFile>>AMark;         //Reads in next Mark
  58.     }
  59.  return Count;              //Returns Count for NumMarks
  60. } //end GetMarks
  61. //******************************************************************************
  62. void Report (int Marks[])
  63. {
  64.  for (Count=0; Count != Max_Size; Count++)    //For loop to Calculate  Highest & Lowest Marks
  65.  {
  66.     if (Marks[Count]<=100 && Marks[Count]>=0) //Validation
  67.      if (Marks[Count]>HiMark)         //If current array position >  Highest so far
  68.        HiMark = Marks[Count];        //Gets Assigned
  69.      else if (Marks[Count]<LoMark)      //If current array position <  Lowest so far
  70.           LoMark = Marks[Count];     //Gets Assigned
  71.  }
  72.  cout<<"\n \n Final Report"<<endl;            //Displaying  Results
  73.  cout<<"\n \n Highest Mark:"<<(setw(7))<<HiMark<<endl;
  74.  cout<<"\n Lowest Mark:"<<(setw(7))<<LoMark<<endl<<endl;
  75.  for (Count=0; Count != Max_Size; Count++)    //For loop calculates   amount of times a number appears
  76.  {
  77.    if (Marks[Count]<=100 && Marks[Count]>=0)   //Validation
  78.    if (Marks[Count] == Marks[Count+1])     //If Current Position =   Next Position
  79.      cout<<"*";                //Display *
  80.    if (Marks[Count]<=100 && Marks[Count]>=0)   //Validation
  81.    if (Marks[Count] != Marks[Count+1])     //If Current Position !=   Next Position
  82.      cout<<"*"<<Marks[Count]<<endl;      //Display *, Displays Number,   Ends Line
  83.  }
  84. } //end Report


i get this error now when i compile...

************
thread stopped
E:\programming2\assign1.exe
Fault:access violation at 0x410002:
write of address 0xf
**************************

can anyone help :(
  • dr_geek
  • Beginner
  • Beginner
  • User avatar
  • Posts: 62
  • Loc: london town

Post 3+ Months Ago

Rabid Dog wrote:
So i don't think it was to bad that I picked up the fact that it hadn't been told it was void.


Yes: you were barking up the right tree, but the squirrel was jumping into another one. ;-)

Rabid Dog wrote:
Returning 0 could indicate that the function exited successfully :wink:


In C/C++ 0 means false :-(

Rabid Dog wrote:
I think this is study material to be honest.


All the more reason to make the code higher quality. I question who set up the study, because including a sort function like this is definitely not C++. A standard part of the language is the STL, which already has state-of-the-art, highly-optimised sorting built in. Go figure!
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

TRUE = there where errors
FALSE = there weren't any errors

That is why I love programming. There are a million and one ways to perceive something. Yeha as for the squirrel, he is road kill! :wink:
  • dr_geek
  • Beginner
  • Beginner
  • User avatar
  • Posts: 62
  • Loc: london town

Post 3+ Months Ago

venetian wrote:
i get this error now when i compile...

************
thread stopped
E:\programming2\assign1.exe
Fault:access violation at 0x410002:
write of address 0xf
**************************

can anyone help :(


It's your call to SortArray(). It has to match the declaration in insort.h.
  • dr_geek
  • Beginner
  • Beginner
  • User avatar
  • Posts: 62
  • Loc: london town

Post 3+ Months Ago

Rabid Dog wrote:
TRUE = there where errors
FALSE = there weren't any errors

That is why I love programming. There are a million and one ways to perceive something.


Yes, but we don't love staying up all night debugging code. :-(

So I trust you'd have a BIG comment letting us in on this lateral thinking ;-)
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Comments are the life blood of my applications! If it weren't for my comments I would probably get lost!

So in reply to your question - Yes a nice BIG FAT Multi line comment alla
/*
*/

:wink:
  • venetian
  • Born
  • Born
  • venetian
  • Posts: 4
  • Loc: Australia

Post 3+ Months Ago

thanks for all the help!

i worked it out..

instead of having the include intsort.h

i just copied and pasted it to the main code..
it works fine now..
thanks.

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 125 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.