| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help needed programme 6
hi got another programme here, need alot of help:
[/* CH9A02.cpp */ /* Calculates average sales */ #include <stdio.h> #include <conio.h> void main () { /* Description ...... Open input file Display headings Initialise grand total FOR each person Initialise person total FOR each sales figure Read sales Accumulate person total and grand total Calculate and display person average Calculate and display overall average Close input file */ /* const and variable declarations */ const int num_of_sales_people = 5, num_of_sales_figs = 6 ; int person_num, sales_num ; float sales_figure, person_total, person_average, grand_total, overall_average ; FILE *inpfile = fopen ("figures.txt", "r") ; getch () ; } ] this programme should read the text file figures.txt which contains the following info : 40 60 70 70 60 60 30 32 23 34 48 52 59 45 43 95 52 61 55 71 73 42 75 69 20 32 76 56 56 81 the output shud be displayed on screen as follows : SALES FIGURES : person average 1 60.00 2 36.50 3 59.17 4 64.17 5 53.50 overall average is 54.67 ![]() |
|
#2
|
|||
|
|||
|
What do you need help on, on starting with or what?
|
|
#3
|
||||
|
||||
|
It looks like you want us to turn pseudo code into an actual C/++ program.
Write what you can then ask for help....
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#4
|
||||
|
||||
|
Boy Bcon you need to just make an auto response for that one
No offense munnsie1, we just get a lot of homework assignments on this site , most of them were ppl dont want to even attempt the assignment not saying your one of them just saying though ![]()
__________________
---Official Member Of The Itsacon Fan Club--- ![]() Give a man a fish and he will eat for a day. Teach a man to fish and he will sit in a boat all day drinking beer. |
|
#5
|
||||
|
||||
|
Quote:
![]() |
|
#6
|
|||
|
|||
|
heres exactly what i cn do:
[/* CH9A02.cpp */ /* Calculates average sales */ #include <stdio.h> #include <conio.h> void main () { /* Description ...... Open input file Display headings Initialise grand total FOR each person Initialise person total FOR each sales figure Read sales Accumulate person total and grand total Calculate and display person average Calculate and display overall average Close input file */ /* const and variable declarations */ const int num_of_sales_people = 5, num_of_sales_figs = 6 ; int person_num, sales_num ; float sales_figure, person_total, person_average, grand_total, overall_average ; FILE *inpfile = fopen ("CH9A02.txt", "r") ; printf ("SALES FIGURES\n") ; for (person_num = 1 ; person_num <= 5 ; ++ person_num) { fscanf (inpfile, "%d" , &person_average) ; printf ("%2d", person_num) ; getch () ; }] please help |
|
#7
|
||||
|
||||
lol, sorry my mistake munnsie1 I didn't see that snippet of code on your first post. my bad. Well First off, could you give me the exact text file, and if what you wrote in your first post is the exact then which line is which, I mean the names in all. And is this suppose to be in C only or C++ as well? |
|
#8
|
|||
|
|||
|
what i wrote above was the exact text file, and it is ust for C++. Thanks for replying, please help!!!
|
|
#9
|
||||
|
||||
|
alright man, let me give it a shot
get back to you in about an hour or so, gotta eat some lunch and then Ill get on it. And no problem ![]() |
|
#10
|
||||
|
||||
|
Ok I have a simple solution to your problem, but keep in mind this is only help to get you on your way, because I code in almost all C++ and use as little C as I have to this might look odd to you.
but hopefully not. I am sure there is better ways to accomplish this but it gets the job done. I wrote a simple program that opens the origanal file then outputs the data to a seperate file called sales figures.txt. If it is suppose to be all on one text then you are going to have to change the code dramatically but if there is no specification then you should be fine. You must continue on what I did as I only did one person in the .txt file. so you will need to complete the rest. I wrote this with Dev-c++ so you might have to include other libraries but I will show you all that I had to include which were only <iostream> and <fstream>Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
/* My way is probably not the most proficient but it gets the job done */
using namespace std;
int main()
{
int p1[5];//person one sales one through six
int p2[5];//next person =)
float p1_average;
float p2_average;
char SF[] = "Sales Figures";
ifstream file;//object of the fstream library for reading files
file.open("CH9A02.txt");//opens file to read and write
if(!file.good())//just a simple error check
{
cout<<"Could not open file properly \n";
}
/* reads and calculates person one */
file>>p1[0]>>p1[1]>>p1[2]>>p1[3]>>p1[4]>>p1[5];//gets data from file
/* the next line of code is not necessary unless you want to show the data it found */
cout<<p1[0]<<" "<<p1[1]<<" "<<p1[2]<<" "<<p1[3]<<" "<<p1[4]<<" "<<p1[5]<<endl;
p1_average = (p1[0] + p1[1] + p1[2] + p1[3] + p1[4] + p1[5]) / 6;
/* this is also not necessary either, only shows results of the averaged sales */
cout<<p1_average<<endl;
/* continue this proccess for each person */
/* makes an object of the ofstream to print data */
ofstream averages;
averages.open("SALES FIGURES.txt");
if(!averages.is_open())//checks for errors just use is_open() instead of good()
{
cout<<"Could not open file correctly \n";
}
averages<<"\t\t\t"<<SF<<endl<<endl;//sends out put data to SALES FIGURES.txt file
/* the tabs are to center SF at the center of the document */
averages<<"Person one's Sales average : "<<p1_average<<endl;
/* continue this for each person */
averages.close();
file.close();
system("PAUSE");
return EXIT_SUCCESS;
}
And thats the jist of it, if you need explanation on any of it let me know and I will get back to you asap, I have today off so hopefully it will be today ![]() NOTE: This way is a little redundant and if you had more lines of text should probably be executed with a for loop to save on space and time, but being that the text is so small I just did it the long way ![]() Last edited by Geo.Garnett : December 12th, 2005 at 01:17 PM. |
|
#11
|
|||
|
|||
|
i appreciate the effort m8, but do not understand any of tht, as i am just starting c++, so only understand basic functions of this language only. could you possibly do it in this for me?
|
|
#12
|
||||
|
||||
|
lol sorry about that. I'm not sure if I can write it much different because I'm self taught and your teacher probably is starting at the beginning with you and with me I just skipped the basics, because I dont like C that much, although C++ is just its superset, I just dont like using the old stuff. So what I will do is explain what I wrote and hopefully that's a help.
Working with text files 101, j/k Well first line the int p1[5] array is the array of integers that will store your data from the text file. second line float p1_average is the average of the sales for p1- This is a float and not an int so you can have a decimal place, but Im sure you already know that. third the ifstream file is an object of the ifstream library. (which basically allows you to open your file) ifstream is for reading and ofstream later will be for output. remember i for input o for output =) next file.open("CH9A02.txt") opens the specific file name which is basically the same thing you did with FILE *inpfile = fopen ("CH9A02.txt", "r") ; just with C++ the updated fstream and iostream already know that if you make an object of the ifstream that its for reading. next file>>p1[0]>>etc. That is basically a C++ version of scanf() just uses the >> operators like cout and cin if you are not familiar with those yet, they are basically methods of inputing and outputing data. cin & cout are for I/O to the program and file(or the object file) inputing to your specific file. next p1_average = (p1[0] etc.)/6 is just the formula to get the average of sales. Now on to the output file.... ofstream averages; is an object of the fstream library that allows you to output your data averages.open("SALES FIGURES.txt") is just the name of the file it makes. averages<< is the same concept of the file version only notice the << this means your printing data to that file. >> means inputing. averages.close() file.close() closes both files system ("PAUSE"); stops the program in order to show results, this is not necessary. So basically to make a long story short is, is that I suck at basics of C so if my teacher asked me to write a C program I would probably do very poorly. So after all this Im sure you are no further. But basically the concept is the same you just need to do it with what you already know. And that is this. make an object FILE * to read and write to a file make variables to store your scanf() then calculate them, and then write the data back to the file. Sounds simpler than it is, but really once you understand the concept of how those things function you will kick yourself because of its simplicity. |
|
#13
|
|||
|
|||
|
thnax. but im still very very stuck, can someone code it in c++ for me please??
![]() |
|
#14
|
|||
|
|||
|
Still stuck somebody plz help!!!
hi got another programme here, need alot of help:
[/* CH9A02.cpp */ /* Calculates average sales */ #include <stdio.h> #include <conio.h> void main () { /* Description ...... Open input file Display headings Initialise grand total FOR each person Initialise person total FOR each sales figure Read sales Accumulate person total and grand total Calculate and display person average Calculate and display overall average Close input file */ /* const and variable declarations */ const int num_of_sales_people = 5, num_of_sales_figs = 6 ; int person_num, sales_num ; float sales_figure, person_total, person_average, grand_total, overall_average ; FILE *inpfile = fopen ("figures.txt", "r") ; getch () ; } ] this programme should read the text file figures.txt which contains the following info : 40 60 70 70 60 60 30 32 23 34 48 52 59 45 43 95 52 61 55 71 73 42 75 69 20 32 76 56 56 81 the output shud be displayed on screen as follows : SALES FIGURES : person average 1 60.00 2 36.50 3 59.17 4 64.17 5 53.50 overall average is 54.67 (has 2 be coded in borland c++)) ![]() Last edited by B-Con : December 15th, 2005 at 12:13 AM. Reason: Thread merged -- no need for a duplicate |
|
#15
|
||||
|
||||
|
Well m8 that is C++, what your doing is C, just to be clear.
and borland c++ is not a language just to be clearer. This sounds like a job for your instructor/teacher So he can clarify your questions. ![]() Last edited by Geo.Garnett : December 13th, 2005 at 08:38 PM. |