Jump to content
HybridZ

Any Programmers here?


datsun40146

Recommended Posts

I have an assignment to complete and I'm stuck on part of the code. The assignment is in C++ and were are to take a set of numbers from a txt file example below, and place them in a csv file. The csv file is supposed to have an average of the numbers, and tell the user if the average was outside a range given by the user. I can't figure this one out can any help me out?

example of txt file

http://www.cs.uky.edu/~ryan/CS221_S08/labs/scores1.txt

 

example of output file

http://www.cs.uky.edu/~ryan/CS221_S08/labs/scores1.csv

I thought I would ask, I think there are a few programmers here.

Link to comment
Share on other sites

I'm a programmer, but it's been a long, long time since I've used C++.

 

Algorithm-wise this is a pretty easy problem. Solve it in baby-steps. First read the line and calculate the average. Then generate the CSV. Finally add in the "Above/Below" comparison.

 

PS - I see you're at UK. I'll try not to hold your association with Clyde against you (Texas A&M grad here).

Link to comment
Share on other sites

Oh that's who you meant I didn't know him as clyde.BTW thanks for the great coach. Would you like tubby? I'm sure he move there lol. Well to answer your tip, our professor would like us not use arrays. Howver we can use loops and just about anything else beside arrays.

Link to comment
Share on other sites

To read the file you can use something like this:

 

//read file

short num = 0;

FILE *ifp;

if ((ifp = fopen("c:scores1.txt", "r")) != NULL)

{

fscanf(ifp,"%d",&num);

fclose(ifp);

}

 

You can use fscanf as many times as you need to get all the values. You can read more about fscanf here http://www.cplusplus.com/reference/clibrary/cstdio/fscanf.html or plenty of other locations as well.

Link to comment
Share on other sites

Read in the txt file one character at a time and reconstruct the data that is there. Some simple case structures will help you read in the numbers and store them in an array. Doing the operations on them once you have read them in should be easy. This may sound too complicated but it shouldn't actually be too bad.

Link to comment
Share on other sites

Strtok is the function that splits a string into "tokens" based on the delimiters you give it. Basically you give it a string (a line from your file), and the characters that separate words (in your case the space char) and it gives you all the words in your string.

 

http://www.cplusplus.com/reference/clibrary/cstring/strtok.html

Link to comment
Share on other sites

Hopefully BG will deliver the results that you guys expect. I couldn't be happier he's gone. Turgeon is doing great stuff with our team. :D

 

Sounds like you're getting advice from people with more C++ experience so I'll duck out of the conversation now.

 

Oh that's who you meant I didn't know him as clyde.BTW thanks for the great coach.
Link to comment
Share on other sites

I'm a programmer and use C++ everyday.

Is your prof wanting you to use the object oriented features of the C++ runtime library or not?

 

You could use the iostream classes if thats OK with him.

This block of code would end up copying file "from" to file "to"

 

#include

ifstream source("from") ;

if ( !source ) error("unable to open 'from' for input");

ofstream target("to") ;

if ( !target ) error("unable to open 'to' for output");

char c ;

while ( target && source.get© ) target.put© ;

 

I know this doesn't answer your entire question but it should help with the file IO portion of the problem.

 

Wheelman

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...