New Posts  All Forums:Forum Nav:

More C++ Help - Page 2

post #11 of 20
Thread Starter 
It didn't work the first time I tried but now it did. I must have wrote something wrong. Thank You, I could always count on ocn
Rep +
    
CPUMotherboardGraphicsRAM
Intel Core i7 5820k @4.5GHZ Asus Rampage V Extreme  EVGA 980ti classified 64GB Corsair Vengeance 3200MHZ 
Hard DriveOptical DriveCoolingOS
[Samsung 950 Pro 512GB][2X Samsung 840 250GB Ra... 16X LITESCRIDE DVD-RW EK Double Res, Double Pump, GPU, CPU combo Windows 10 x64 
MonitorKeyboardPowerCase
55IN 4K  Ducky Shine Corsair RM 1000i Modular STH10 
Mouse
Corsair m95 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 5820k @4.5GHZ Asus Rampage V Extreme  EVGA 980ti classified 64GB Corsair Vengeance 3200MHZ 
Hard DriveOptical DriveCoolingOS
[Samsung 950 Pro 512GB][2X Samsung 840 250GB Ra... 16X LITESCRIDE DVD-RW EK Double Res, Double Pump, GPU, CPU combo Windows 10 x64 
MonitorKeyboardPowerCase
55IN 4K  Ducky Shine Corsair RM 1000i Modular STH10 
Mouse
Corsair m95 
  hide details  
Reply
post #12 of 20
You could probably use some sort of loop. Are you allowed to use an array?
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
post #13 of 20
Quote:
Originally Posted by skatingrocker17 View Post
You could probably use some sort of loop. Are you allowed to use an array?
Ah good idea. That's technically still one variable.
post #14 of 20
Quote:
Originally Posted by skatingrocker17 View Post
You could probably use some sort of loop. Are you allowed to use an array?
yeah this should work. I believe off top of my head its a for loop, but it might be a while. You have user enter and while i think file is still open the user can add stuff and than give the user option to save and close.
Precious 2.0
(15 items)
 
My precious
(16 items)
 
 
CPUMotherboardGraphicsRAM
R7 1700 @ 3.9 | 1.35v Asus VI Crosshair Hero EVGA Titan X Hybrid 1514/2000 32GB Gskill DDR4 3200 TridentZ 
Hard DriveOptical DriveCoolingOS
Samsung 840PRO 256GB LG 10x Blu-ray Burner Swiftech H220-X Window 8.1 Professional 64Bit 
MonitorMonitorKeyboardPower
Crossover 27inch Acer XB270HU 144Hz G-Sync Deck Legend tactile frost EVGA G2 750W 
CaseMouseAudio
Coolmaster Haf 932 DeathAddder 3.5 Black edition Titanium HD | O2 DAC + AMP 
CPUMotherboardGraphicsRAM
Intel i7-970 @ 4.2 Asus Rampage Formula III Asus DC2 R9 290X @ 1110 24GB of Samsung DDR3 @ 1866 
Hard DriveOptical DriveCoolingOS
Samsung 840PRO 256GB | 2x Samsung F4 2TB | WD 6... LG 10x Blu-ray Burner H70 with 2x GT2150 Window 8.1 Professional 64Bit 
MonitorKeyboardPowerCase
Dell 2407WHFP-HC and Crossover 27Q LED-P Deck Legend tactile frost Seasonic 700watt M12 Coolmaster Haf 932 
MouseMouse PadAudioAudio
DeathAddder 3.5 Black edition TeamLiquid Goliathus Titanium HD AudioEngine A2 and ATH-700D 
CPUGraphicsRAMHard Drive
Intel - 3630QM Nvidia GTX 680M 8 GB DDR3 1600 500GB 7200 | 128 GB Samsung 840 Pro 
Optical DriveOS
DVD RW  Windows 8 Pro 
  hide details  
Reply
Precious 2.0
(15 items)
 
My precious
(16 items)
 
 
CPUMotherboardGraphicsRAM
R7 1700 @ 3.9 | 1.35v Asus VI Crosshair Hero EVGA Titan X Hybrid 1514/2000 32GB Gskill DDR4 3200 TridentZ 
Hard DriveOptical DriveCoolingOS
Samsung 840PRO 256GB LG 10x Blu-ray Burner Swiftech H220-X Window 8.1 Professional 64Bit 
MonitorMonitorKeyboardPower
Crossover 27inch Acer XB270HU 144Hz G-Sync Deck Legend tactile frost EVGA G2 750W 
CaseMouseAudio
Coolmaster Haf 932 DeathAddder 3.5 Black edition Titanium HD | O2 DAC + AMP 
CPUMotherboardGraphicsRAM
Intel i7-970 @ 4.2 Asus Rampage Formula III Asus DC2 R9 290X @ 1110 24GB of Samsung DDR3 @ 1866 
Hard DriveOptical DriveCoolingOS
Samsung 840PRO 256GB | 2x Samsung F4 2TB | WD 6... LG 10x Blu-ray Burner H70 with 2x GT2150 Window 8.1 Professional 64Bit 
MonitorKeyboardPowerCase
Dell 2407WHFP-HC and Crossover 27Q LED-P Deck Legend tactile frost Seasonic 700watt M12 Coolmaster Haf 932 
MouseMouse PadAudioAudio
DeathAddder 3.5 Black edition TeamLiquid Goliathus Titanium HD AudioEngine A2 and ATH-700D 
CPUGraphicsRAMHard Drive
Intel - 3630QM Nvidia GTX 680M 8 GB DDR3 1600 500GB 7200 | 128 GB Samsung 840 Pro 
Optical DriveOS
DVD RW  Windows 8 Pro 
  hide details  
Reply
post #15 of 20
Thread Starter 
This was my final program to open the file:
Code:
/*Tekla Alleyne
Mr.Eiholzer 1/23/11
C++ 1B*
Proplem 23 Pg 156*/
 //Part 1

//This program saves floating point values to a file

//Used fstream so the program can understand ofstream 

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
//Variables for the Floating Point Numbers 
ofstream outputFile;

//Each Variable represents a Floating Point Number
double Num;

//floating_Point.txt will be located in the directory that this program is located
outputFile.open("floating_Point.txt"); //Open file to save values

cout << "Your File Will Be Saved In The Same Directory That This Program Is Located"<<endl;
cout<<endl;
cout << "Please Enter Five Floating Point Values " <<endl;

// Floating Point Numbers that needs to be entered
//The output will be located in the floating_point.txt file
cin >>Num;
outputFile << Num <<endl;

cin >>Num;
outputFile << Num <<endl;

cin >>Num;
outputFile << Num<<endl;

cin >>Num;
outputFile << Num <<endl;

cin >>Num;
outputFile << Num <<endl;


//Closing File
outputFile.close();
cout << "Thank You\
";


   
   cout <<system("pause");
   return 0;
}
Now I'm supposed to open it and add all the values and this is what I got:

Code:
/*Tekla Alleyne
Mr.Eiholzer 1/23/11
C++ 1B*
Proplem 23 Pg 156*/


//This program Opens floating point values to a file

//Used fstream so the program can understand ofstream 

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
//Variables for the Floating Point Numbers 
ifstream inFile;
double Num;

inFile.open("floating_Point");
cout << "Reading Information From The Saved File."<<endl;
cout<<endl;

//Displays the floating point valueas read from the file floating_Point

inFile >>Num; //Reads first Value
cout << Num <<endl;

inFile >>Num;  //Reads second Value
cout << Num <<endl;

inFile >>Num;  //Reads third Value
cout << Num<<endl;

inFile >>Num; //Reads fourth Value
cout << Num <<endl;

inFile >>Num;  //Reads Fifth Value
cout << Num <<endl;
cout<<endl;

//Adds the floating point Values
cout <<"Total Value= " << Num+Num+Num+Num+Num <<endl;
cout<<endl;
inFile.close();
cout << "Thank You\
";


   
   cout <<system("pause");
   return 0;
}
Somehow I think it won't work
    
CPUMotherboardGraphicsRAM
Intel Core i7 5820k @4.5GHZ Asus Rampage V Extreme  EVGA 980ti classified 64GB Corsair Vengeance 3200MHZ 
Hard DriveOptical DriveCoolingOS
[Samsung 950 Pro 512GB][2X Samsung 840 250GB Ra... 16X LITESCRIDE DVD-RW EK Double Res, Double Pump, GPU, CPU combo Windows 10 x64 
MonitorKeyboardPowerCase
55IN 4K  Ducky Shine Corsair RM 1000i Modular STH10 
Mouse
Corsair m95 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 5820k @4.5GHZ Asus Rampage V Extreme  EVGA 980ti classified 64GB Corsair Vengeance 3200MHZ 
Hard DriveOptical DriveCoolingOS
[Samsung 950 Pro 512GB][2X Samsung 840 250GB Ra... 16X LITESCRIDE DVD-RW EK Double Res, Double Pump, GPU, CPU combo Windows 10 x64 
MonitorKeyboardPowerCase
55IN 4K  Ducky Shine Corsair RM 1000i Modular STH10 
Mouse
Corsair m95 
  hide details  
Reply
post #16 of 20
Here are some problems I see right away:

1.) In your code that reads the numbers from the file, you don't have ".txt" in your inFile.open statement

2.) In your code that reads the numbers from the file and adds them up, you are repeatedly storing the numbers into "num". At the end of reading all 5, you try to add them up by saying num + num + num + num + num. This will not work. "num" is a single variable. If you say:

num = 5;
num = 2;
cout << num + num;

You will get 4. Once you say "num = 2" it will overwrite whatever was in num previously.

What you need to do is either create 5 separate variables, one to hold each of the 5 numbers (not recommended) or create a loop structure that reads in all 5 numbers and each time it reads a number in, it updates a "sum" variable. Like this:

Code:
double num, sum;
sum = 0.0;
while(inFile >> num)
{
   cout << num << endl;
   sum += num;
}
The loop does this: While there is still a number to read from the file, read in a number from the file into "num", print that number, and then update our "sum" variable.

Now just print out the sum variable and I believe this works.

3.) I would suggest changing the first program as well, to be a bit more dynamic. Use a loop structure to get the 5 numbers from the user, instead of brute-force, like this:

Code:
cout << "Please enter five floating point number " << endl;
int i;
for(i = 0; i < 5; i++)
{
   cin >> Num;
   outputFile << Num << endl;
}
4.) When you open a file for reading or writing, check to make sure it opened correctly.

************************************************** ********************************
You can also make this a bit more robust in the future, if it doesn't go against the instructions for the assignment: Allow the user to enter in as many numbers as they would like. The program knows that the user has finished enter in numbers when a programmer-defined sentinel value has been entered, such as -99 or whatever you think is intuitive. It would look like this instead of that above for loop:

Code:
cout << "Please enter floating point numbers (enter -99 to quit) " << endl;
cin >> Num;
while(Num != -99)
{
   outputFile << Num << endl;
   cin >> Num;
}
That will work nicely if you've modified the second program to read from the file until there are no more numbers.

Of course, this may not be what is required, but it's just something you may encounter later
Edited by Tomiger - 1/24/11 at 7:38pm
 
Mineral Oil PC
(7 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4790K Devil's Canyon 4.0GHz ASUS Sabertooth Z97 Mark S (White) Zotac GTX 760 Avexir Raiden 16GB DDR3 
Hard DriveCoolingCoolingCooling
OCZ Trion 240GB XSPC Raystorm Pro (white) XSPC EX 240 (white) XSPC EX 360 (white) 
CoolingCoolingCoolingCooling
Swiftech MCP655 Monsoon Dual Bay Reservoir Monsoon Modular Tube Reservoir Monsoon Stand Alone Pump Top/Cover 
CoolingPower
EK GTX 970 Water Block Corsair AX760i 
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K Sandy Bridge 3.4 GHz Gigabyte GA-P67A-UD4-B3 GTX 285 GTX 285 
RAMHard DriveHard DriveCooling
Corsair Vengeance 16GB Western Digital 1TB Western Digital 250GB EK-Supremacy Clean CSQ - Nickel 
CoolingCoolingCoolingCooling
XSPC RX 360 XSPC EX 360 (cross flow) FrozenQ T-virus Reservoir EK Waterblock GTX 285 
Cooling
Swiftech MCP655 
  hide details  
Reply
 
Mineral Oil PC
(7 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4790K Devil's Canyon 4.0GHz ASUS Sabertooth Z97 Mark S (White) Zotac GTX 760 Avexir Raiden 16GB DDR3 
Hard DriveCoolingCoolingCooling
OCZ Trion 240GB XSPC Raystorm Pro (white) XSPC EX 240 (white) XSPC EX 360 (white) 
CoolingCoolingCoolingCooling
Swiftech MCP655 Monsoon Dual Bay Reservoir Monsoon Modular Tube Reservoir Monsoon Stand Alone Pump Top/Cover 
CoolingPower
EK GTX 970 Water Block Corsair AX760i 
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K Sandy Bridge 3.4 GHz Gigabyte GA-P67A-UD4-B3 GTX 285 GTX 285 
RAMHard DriveHard DriveCooling
Corsair Vengeance 16GB Western Digital 1TB Western Digital 250GB EK-Supremacy Clean CSQ - Nickel 
CoolingCoolingCoolingCooling
XSPC RX 360 XSPC EX 360 (cross flow) FrozenQ T-virus Reservoir EK Waterblock GTX 285 
Cooling
Swiftech MCP655 
  hide details  
Reply
post #17 of 20
Thread Starter 
Quote:
Originally Posted by Tomiger View Post
Here are some problems I see right away:

1.) In your code that reads the numbers from the file, you don't have ".txt" in your inFile.open statement

2.) In your code that reads the numbers from the file and adds them up, you are repeatedly storing the numbers into "num". At the end of reading all 5, you try to add them up by saying num + num + num + num + num. This will not work. "num" is a single variable. If you say:

num = 5;
num = 2;
cout << num + num;

You will get 4. Once you say "num = 2" it will overwrite whatever was in num previously.

What you need to do is either create 5 separate variables, one to hold each of the 5 numbers (not recommended) or create a loop structure that reads in all 5 numbers and each time it reads a number in, it updates a "sum" variable. Like this:

Code:
double num, sum;
sum = 0.0;
while(inFile >> num)
{
   cout << num << endl;
   sum += num;
}
The loop does this: While there is still a number to read from the file, read in a number from the file into "num", print that number, and then update our "sum" variable.

Now just print out the sum variable and I believe this works.

3.) I would suggest changing the first program as well, to be a bit more dynamic. Use a loop structure to get the 5 numbers from the user, instead of brute-force, like this:

Code:
cout << "Please enter five floating point number " << endl;
int i;
for(i = 0; i < 5; i++)
{
   cin >> Num;
   outputFile << Num << endl;
}
4.) When you open a file for reading or writing, check to make sure it opened correctly.

************************************************** ********************************
You can also make this a bit more robust in the future, if it doesn't go against the instructions for the assignment: Allow the user to enter in as many numbers as they would like. The program knows that the user has finished enter in numbers when a programmer-defined sentinel value has been entered, such as -99 or whatever you think is intuitive. It would look like this instead of that above for loop:

Code:
cout << "Please enter floating point numbers (enter -99 to quit) " << endl;
cin >> Num;
while(Num != -99)
{
   outputFile << Num << endl;
   cin >> Num;
}
That will work nicely if you've modified the second program to read from the file until there are no more numbers.

Of course, this may not be what is required, but it's just something you may encounter later
Thank you very much. That's exactly what I had to do
    
CPUMotherboardGraphicsRAM
Intel Core i7 5820k @4.5GHZ Asus Rampage V Extreme  EVGA 980ti classified 64GB Corsair Vengeance 3200MHZ 
Hard DriveOptical DriveCoolingOS
[Samsung 950 Pro 512GB][2X Samsung 840 250GB Ra... 16X LITESCRIDE DVD-RW EK Double Res, Double Pump, GPU, CPU combo Windows 10 x64 
MonitorKeyboardPowerCase
55IN 4K  Ducky Shine Corsair RM 1000i Modular STH10 
Mouse
Corsair m95 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 5820k @4.5GHZ Asus Rampage V Extreme  EVGA 980ti classified 64GB Corsair Vengeance 3200MHZ 
Hard DriveOptical DriveCoolingOS
[Samsung 950 Pro 512GB][2X Samsung 840 250GB Ra... 16X LITESCRIDE DVD-RW EK Double Res, Double Pump, GPU, CPU combo Windows 10 x64 
MonitorKeyboardPowerCase
55IN 4K  Ducky Shine Corsair RM 1000i Modular STH10 
Mouse
Corsair m95 
  hide details  
Reply
post #18 of 20
I don't think you had to use the std namespace since you didnt use stroustrups std_libraries_blahblah.h file. I'm just saying. Stroustrup is the chair of computer science here at A&M.
Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 Gigabyte GA-EP45-UD3P Sapphire 5870 Cypress XT EF6 2GB 4GB (2x2GB) G.SKILL 1066 
Hard DriveOptical DriveOSMonitor
32GB Corsair SSD + 2x 320 + 1x 250 Lightscribe drive Windows 7 Ultimate Hanns-G HG-216DPO 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard OCZ GameXStream 850W HAF 932 Logitech G5 Laser Gaming Mouse 
Mouse Pad
Allsop 
  hide details  
Reply
Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 Gigabyte GA-EP45-UD3P Sapphire 5870 Cypress XT EF6 2GB 4GB (2x2GB) G.SKILL 1066 
Hard DriveOptical DriveOSMonitor
32GB Corsair SSD + 2x 320 + 1x 250 Lightscribe drive Windows 7 Ultimate Hanns-G HG-216DPO 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard OCZ GameXStream 850W HAF 932 Logitech G5 Laser Gaming Mouse 
Mouse Pad
Allsop 
  hide details  
Reply
post #19 of 20
Ignore this post. I completely misunderstood the point of it.
Edited by floatingDivs - 2/1/11 at 5:27pm
post #20 of 20
Use classes and confuse your teacher!




class Floaters {
int float1,float2,float3,float4,float5;

public:

Floaters();
~Floaters();

int floats(int infloater1, int infloater2, int infloater3, int infloater4, int infloater5);

Unto The Void
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 6800k Gigabyte G1 X99-Phoenix SLI Gigabyte GTX 1080 G1 32GB RipJaw 4 DDR4-3000 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 500GB Crucial M4 2 x Spinpoint F1 Samsung 850 1TB 
CoolingOSMonitorMonitor
Noctua NH-D15S Windows 10 LG Ultrawide 29" Asus 24" 
KeyboardPowerCaseMouse
Logitech 810 EVGA P2 650 Watt In Win 303 Logitech G502 
  hide details  
Reply
Unto The Void
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 6800k Gigabyte G1 X99-Phoenix SLI Gigabyte GTX 1080 G1 32GB RipJaw 4 DDR4-3000 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 500GB Crucial M4 2 x Spinpoint F1 Samsung 850 1TB 
CoolingOSMonitorMonitor
Noctua NH-D15S Windows 10 LG Ultrawide 29" Asus 24" 
KeyboardPowerCaseMouse
Logitech 810 EVGA P2 650 Watt In Win 303 Logitech G502 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming