Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 10-19-09   #1 (permalink)
AMD Overclocker
 
amd nvidia

Join Date: Apr 2009
Posts: 432

Rep: 19 godsgift2dagame Unknown
Unique Rep: 18
Trader Rating: 0
Default Code "not working"

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;
}
I'm not including the files I'm inputting/outputting because I don't want the problem done for me, I just want to know where I made a mistake. I'm guessing, based off what's missing (everything but the first numbers and "haha" for i = 0 ) is missing.
__________________
System: $775
CPU
Phenom II 720
Motherboard
Gigabyte MA790X-UD4P
Memory
4GB OCZ Reaper DDR2 1150
Hard Drive
250GB Seagate Barracuda 7200.10 x 2
Power Supply
PC Power & Cooling 610W
Case
Antec 300
CPU cooling
XIGMATEK Dark Knight-S1283V
OS
Vista 64-bit
godsgift2dagame is offline   Reply With Quote
Old 10-19-09   #2 (permalink)
With great difficulty
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 5,210

Rep: 614 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 370
FAQs Submitted: 6
Trader Rating: 5
Default

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
__________________
System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 10-19-09   #3 (permalink)
4.0 GHz
 
dharmaBum's Avatar
 
intel nvidia

Join Date: Apr 2007
Location: Raleigh, NC
Posts: 747

Rep: 121 dharmaBum is acknowledged by manydharmaBum is acknowledged by many
Unique Rep: 89
Trader Rating: 0
Default

Code:
double n[] = {0};
creates an array of size 1;

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:
There are only two industries that refer to their customers as ‘users’.

System: Europa
CPU
E8500 4.36ghz @ 1.36v
Motherboard
EVGA 780i SLi P05 Bios
Memory
G.SKILL 4GB(2x2GB) @ 924MHz (5-4-4-12-2T)
Graphics Card
2xEVGA 8800GTS (G92) 512MB @800/2000/2110
Hard Drive
Seagate 500gbx2, (fake-)RAID0
Sound Card
Sound Blaster X-Fi XtremeGamer
Power Supply
CORSAIR 1000HX 1000W
Case
Gigabyte GZ-FA2CA-AJB Black Aluminum
CPU cooling
TDX 775 Block, 360 BlackIce rad
GPU cooling
MAZE5x2, TT copper HS
OS
Fedora10-86_64/Vista64
Monitor
22" Samsung SyncMaster 2232BW
dharmaBum is offline   Reply With Quote
Old 4 Weeks Ago   #4 (permalink)
AMD Overclocker
 
amd nvidia

Join Date: Apr 2009
Posts: 432

Rep: 19 godsgift2dagame Unknown
Unique Rep: 18
Trader Rating: 0
Default

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;
}
__________________
System: $775
CPU
Phenom II 720
Motherboard
Gigabyte MA790X-UD4P
Memory
4GB OCZ Reaper DDR2 1150
Hard Drive
250GB Seagate Barracuda 7200.10 x 2
Power Supply
PC Power & Cooling 610W
Case
Antec 300
CPU cooling
XIGMATEK Dark Knight-S1283V
OS
Vista 64-bit
godsgift2dagame is offline   Reply With Quote
Old 4 Weeks Ago   #5 (permalink)
4.0 GHz
 
dharmaBum's Avatar
 
intel nvidia

Join Date: Apr 2007
Location: Raleigh, NC
Posts: 747

Rep: 121 dharmaBum is acknowledged by manydharmaBum is acknowledged by many
Unique Rep: 89
Trader Rating: 0
Default

Quote:
Originally Posted by godsgift2dagame View Post
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".
Perhaps you could put the "garbage code" here?

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:
There are only two industries that refer to their customers as ‘users’.

System: Europa
CPU
E8500 4.36ghz @ 1.36v
Motherboard
EVGA 780i SLi P05 Bios
Memory
G.SKILL 4GB(2x2GB) @ 924MHz (5-4-4-12-2T)
Graphics Card
2xEVGA 8800GTS (G92) 512MB @800/2000/2110
Hard Drive
Seagate 500gbx2, (fake-)RAID0
Sound Card
Sound Blaster X-Fi XtremeGamer
Power Supply
CORSAIR 1000HX 1000W
Case
Gigabyte GZ-FA2CA-AJB Black Aluminum
CPU cooling
TDX 775 Block, 360 BlackIce rad
GPU cooling
MAZE5x2, TT copper HS
OS
Fedora10-86_64/Vista64
Monitor
22" Samsung SyncMaster 2232BW
dharmaBum is offline   Reply With Quote
Old 4 Weeks Ago   #6 (permalink)
AMD Overclocker
 
amd nvidia

Join Date: Apr 2009
Posts: 432

Rep: 19 godsgift2dagame Unknown
Unique Rep: 18
Trader Rating: 0
Default

God dang. LOL still working on code.
__________________
System: $775
CPU
Phenom II 720
Motherboard
Gigabyte MA790X-UD4P
Memory
4GB OCZ Reaper DDR2 1150
Hard Drive
250GB Seagate Barracuda 7200.10 x 2
Power Supply
PC Power & Cooling 610W
Case
Antec 300
CPU cooling
XIGMATEK Dark Knight-S1283V
OS
Vista 64-bit
godsgift2dagame is offline   Reply With Quote
Old 4 Weeks Ago   #7 (permalink)
Security Sleuth
 
Pooping^fish's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: egypt
Posts: 1,267

Rep: 67 Pooping^fish is acknowledged by some
Unique Rep: 62
Trader Rating: 3
Default

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:
"O, hai! Want som pRon? Dwnlod ths kodk frst. Its teh bst pRonz ever, we prmis." -GibbyGano
Proud Member of the Linux Gaming Community
I am your friend.

System: ragequit
CPU
Q9550 4ghz @ 1.25v
Motherboard
Asus Max 2 formula
Memory
OCZ LV blade 1:1 950mhz
Graphics Card
8800gtx 610/1ghz
Hard Drive
7200.10 250gb
Sound Card
X-FI Extreme Music
Power Supply
750w Toughpower
Case
Lian li pc-65
CPU cooling
TRUE
GPU cooling
stock
OS
leetlinucks
Monitor
24" Westy
Pooping^fish is offline   Reply With Quote
Old 4 Weeks Ago   #8 (permalink)
AMD Overclocker
 
amd nvidia

Join Date: Apr 2009
Posts: 432

Rep: 19 godsgift2dagame Unknown
Unique Rep: 18
Trader Rating: 0
Default

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!
__________________
System: $775
CPU
Phenom II 720
Motherboard
Gigabyte MA790X-UD4P
Memory
4GB OCZ Reaper DDR2 1150
Hard Drive
250GB Seagate Barracuda 7200.10 x 2
Power Supply
PC Power & Cooling 610W
Case
Antec 300
CPU cooling
XIGMATEK Dark Knight-S1283V
OS
Vista 64-bit
godsgift2dagame is offline   Reply With Quote
Old 3 Weeks Ago   #9 (permalink)
Security Sleuth
 
Pooping^fish's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: egypt
Posts: 1,267

Rep: 67 Pooping^fish is acknowledged by some
Unique Rep: 62
Trader Rating: 3
Default

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:
"O, hai! Want som pRon? Dwnlod ths kodk frst. Its teh bst pRonz ever, we prmis." -GibbyGano
Proud Member of the Linux Gaming Community
I am your friend.

System: ragequit
CPU
Q9550 4ghz @ 1.25v
Motherboard
Asus Max 2 formula
Memory
OCZ LV blade 1:1 950mhz
Graphics Card
8800gtx 610/1ghz
Hard Drive
7200.10 250gb
Sound Card
X-FI Extreme Music
Power Supply
750w Toughpower
Case
Lian li pc-65
CPU cooling
TRUE
GPU cooling
stock
OS
leetlinucks
Monitor
24" Westy
Pooping^fish is offline   Reply With Quote
Old 3 Weeks Ago   #10 (permalink)
"Ghetto Solutions"
 
AMD+nVidia's Avatar
 
intel nvidia

Join Date: Apr 2006
Location: My Room
Posts: 6,233

Rep: 302 AMD+nVidia is a proven memberAMD+nVidia is a proven memberAMD+nVidia is a proven memberAMD+nVidia is a proven member
Unique Rep: 235
Folding Team Rank: 52
Trader Rating: 15
Default

Quote:
Originally Posted by Pooping^fish View Post
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.
My programming classes in college are SO BS.
__________________
Do you really think the iPhone isn't that big? Think Again

System: WaterWorks
CPU
Core 2 Quad Q6700 (3.79GHz)
Motherboard
ASUS Maximus Formula X38
Memory
6GB Dual Channel PC6400 5-4-4-12 (800MHz)
Graphics Card
PNY XLR8 GTX280 1024MB
Hard Drive
1.18TB RAID 0 + 500GB RAID 0 + 400GB Spare
Sound Card
Modded Creative X-Fi XtremeMusic
Power Supply
Corsair 900w
Case
Cooler Master Cosmos 1000 (Modded)
CPU cooling
Custom V6 Stinger *Lapped*
GPU cooling
Stock
OS
Windows 7 Ultimate x64
Monitor
I-INC 28" HDMI 1920x1200
3 Million+ Folding at Home points
AMD+nVidia is offline I fold for Overclock.net Overclocked Account   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 04:02 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.13406 seconds with 8 queries