datsun40146 Posted April 2, 2008 Share Posted April 2, 2008 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. Quote Link to comment Share on other sites More sharing options...
pete280z Posted April 2, 2008 Share Posted April 2, 2008 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). Quote Link to comment Share on other sites More sharing options...
datsun40146 Posted April 2, 2008 Author Share Posted April 2, 2008 The problem I'm having is just that reading it in from the txt file. I can't use the getline function I don't think, so I'm stuck. Clyde's at UK? If so then we have to meet up. Quote Link to comment Share on other sites More sharing options...
pete280z Posted April 2, 2008 Share Posted April 2, 2008 Ah, gotcha. I don't remember the API well enough to give specific advice. Could you use getline and then split the line into an array of the individual entries using another method? Clyde AKA Billy Clyde Gillispie Quote Link to comment Share on other sites More sharing options...
datsun40146 Posted April 2, 2008 Author Share Posted April 2, 2008 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. Quote Link to comment Share on other sites More sharing options...
SteelToad Posted April 2, 2008 Share Posted April 2, 2008 Use the force Luke ... if that doesn't work, try: strtok Quote Link to comment Share on other sites More sharing options...
datsun40146 Posted April 2, 2008 Author Share Posted April 2, 2008 and what does that do steel? Quote Link to comment Share on other sites More sharing options...
Bartman Posted April 2, 2008 Share Posted April 2, 2008 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. Quote Link to comment Share on other sites More sharing options...
BobbyZ Posted April 2, 2008 Share Posted April 2, 2008 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. Quote Link to comment Share on other sites More sharing options...
SteelToad Posted April 2, 2008 Share Posted April 2, 2008 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 Quote Link to comment Share on other sites More sharing options...
pete280z Posted April 2, 2008 Share Posted April 2, 2008 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. 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. Quote Link to comment Share on other sites More sharing options...
wheelman Posted April 2, 2008 Share Posted April 2, 2008 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 Quote Link to comment Share on other sites More sharing options...
datsun40146 Posted April 3, 2008 Author Share Posted April 3, 2008 Got it Thanks guys you were a world of help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.