|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Code "not working"
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||
|
AMD Overclocker
|
Hey guys,
__________________I come YET again with another issue. I am trying to use <fstream> to input a file with it's many numbers, sum them up, and output them, as well as the average. Here's what I've got so far. Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
ifstream original;
ofstream update;
original.open("numbers.dat");
if(original.fail())
{
cout << "Failure to access input file. ";
exit(1);
}
update.open("updated.dat");
if(update.fail())
{
cout << "Unable to access output file. ";
exit(1);
}
double n[] = {0};
int scores = 5;
double sum = 0;
double average;
for(int i = 0; i < scores; i++)
{
original >> n[i];
update << n[i] << "haha " << endl;
sum += n[i];
average = sum / scores;
}
update << "The sum of the numbers is "
<< sum
<< "\tThe average is "
<< average;
original.close();
update.close();
return 0;
}
|
|||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
With great difficulty
![]() |
What problem are you having? If you just dump a bunch of code into a post and simply say "it's not working" few people are going to search for the real problem
__________________
|
|||||||||||||
|
|
|
|
#3 (permalink) | ||||||||||||||
|
4.0 GHz
![]() |
Code:
double n[] = {0};
either: i.) use a data structure that allows on-the-fly allocation, or ii.) declare the array the right size.
__________________
3DMark06: 19091 - 3DMark Vantage: P15264 - SuperPi: 10.968s Programming Quote of the Day: Bjarne Stroustrup: Quote:
|
||||||||||||||
|
|
|
|
|
#4 (permalink) | |||||||||
|
AMD Overclocker
|
Basically, the ".dat" file has both numbers and text (it's a bowling thing) and I ONLY WANT the scores to be displayed in the new created file "MGlane9.txt". I've listed them out as integers in hopes of drawing only the numbers, but the new files shows "garbage code".
__________________![]() Code:
#include <fstream>
#include <iostream>
int main()
{
using namespace std;
ifstream initial_data;
ofstream final_data;
initial_data.open("lane9.dat");
if(initial_data.fail())
{
cout << "Unable to open lane9.dat";
exit(1);
}
final_data.open("MGlane9.txt");
if(final_data.fail())
{
cout << "Unable to open MGlane9.txt";
exit(1);
}
int first, second;
initial_data >> first >> second;
final_data << "The numbers are "
<< first
<< second;
initial_data.close();
final_data.close();
return 0;
}
|
|||||||||
|
|
|
|
|
#5 (permalink) | |||||||||||||||
|
4.0 GHz
![]() |
Quote:
I can't find anything wrong with your code (assuming only numbers in the input file), other than the output isn't putting a space between anything. What are you using to look at the output file?
__________________
3DMark06: 19091 - 3DMark Vantage: P15264 - SuperPi: 10.968s Programming Quote of the Day: Bjarne Stroustrup: Quote:
|
|||||||||||||||
|
|
|
|
|
#6 (permalink) | |||||||||
|
AMD Overclocker
|
God dang. LOL still working on code.
__________________
|
|||||||||
|
|
|
|
|
#7 (permalink) | ||||||||||||||
|
Security Sleuth
![]() |
Manually parse the file. Read it in while checking for your delimiter (i.e., a newline).
atoi() the value (assuming a number) and throw that in a var, struct whatever, then output it retaining correct formatting. Thats part of why I dont like C++ for new coders. << / >> isnt doing everything for you. Learn whats going on.
__________________
Quote:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
|
|
#8 (permalink) | |||||||||
|
AMD Overclocker
|
Hey poop
__________________ ,I am not 100% sure, but I think I'm not allowed to use code OUTSIDE of what is listed in the book by that chapter, in this case 6. Basically ifstream and ofstream. I used, for instance in week 2, printf and had points taken off because it wasn't cout and cin. If I can't get it, I'll "settle " for your idea. Thanks a bunch, man!
|
|||||||||
|
|
|
|
|
#9 (permalink) | ||||||||||||||
|
Security Sleuth
![]() |
Wow, really? Thats incredibly cheap.
Yet another instance of a student trying to take the initiative to further their selves and the teacher cuts them off for it? Thats bull****. Glad Ive done things myself.
__________________
Quote:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
|
|
#10 (permalink) | |||||||||||||
|
"Ghetto Solutions"
![]() |
My programming classes in college are SO BS.
__________________
Do you really think the iPhone isn't that big? Think Again
|
|||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|