New Posts  All Forums:Forum Nav:

More C++ Help

post #1 of 20
Thread Starter 
I must write a program that asks the user to enter five floating point numbers and then save it to a file. My teacher says that I must do so only using 1 variable. I can easily accomplish this if I could use more than one variable but I can't. So Far I have This.

Code:

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


//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
cin >> Num;


//The output will be located in the floating_point.txt file
outputFile <<Num;



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


   
   cout <<system("pause");
   return 0;

    
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 #2 of 20
Save the file sequentially? Ask for input, get the first number, write that number to the file. ask for the next input, get the next number and write it to the same variable, then append that data to the file. Keep going like that.
post #3 of 20
Thread Starter 
you mean like

cin >>num;
outputFile <<num;
cin >>num;
outputFile <<num;

it wouldn't work, I tried it
    
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 #4 of 20
I'm not a C++ guy, so I don't know exactly what commands you'll need.

Is there a way in C++ to add data to something? not just num >> outputFile, but a way to add num to what's already in outputFile
post #5 of 20
create a global variable and have the program actively save/rewrite in the same variable and WA-LA you got what you need
post #6 of 20
Thread Starter 
Quote:
Originally Posted by saw4974 View Post
create a global variable and have the program actively save/rewrite in the same variable and WA-LA you got what you need
I have no idea what that means lol
    
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 #7 of 20
Quote:
Originally Posted by startekee View Post
you mean like

cin >>num;
outputFile <<num;
cin >>num;
outputFile <<num;

it wouldn't work, I tried it
I don't see why this should not work, I use log << value << endl; all the time and I make files that are thousands of lines.
X-Caliber
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core 2 Quad Q8300 @3.0 Ghz Gigabyte EP43 UD3L ATI Radeon HD3850 512MB 800/950 4x1 GB 800 mhz DDR2 (Dual Channel) 
Hard DriveOptical DriveOSMonitor
160 Hitachi HDD LG Super-Multi Drive with SecurDisc Windows 7 - Ultimate (x86) Samsung SyncMaster 2043SWX 20" 1600x900 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard rev. 2 460 watt Coolermaster Coolermaster Elite Razer Deathadder 
Mouse Pad
Steelseries QcK 
  hide details  
Reply
X-Caliber
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core 2 Quad Q8300 @3.0 Ghz Gigabyte EP43 UD3L ATI Radeon HD3850 512MB 800/950 4x1 GB 800 mhz DDR2 (Dual Channel) 
Hard DriveOptical DriveOSMonitor
160 Hitachi HDD LG Super-Multi Drive with SecurDisc Windows 7 - Ultimate (x86) Samsung SyncMaster 2043SWX 20" 1600x900 
KeyboardPowerCaseMouse
Logitech G15 Gaming Keyboard rev. 2 460 watt Coolermaster Coolermaster Elite Razer Deathadder 
Mouse Pad
Steelseries QcK 
  hide details  
Reply
post #8 of 20
ok i'm not sure your teacher would like this but i'm going to write an example program(not completed) to help you out, gimme a lil' bit to find a free compiler and ill post the program.
post #9 of 20
After your first num >>output file, get the user input and write it to the num variable again. then try using fopen +a to write to the end of the file.

fopen
post #10 of 20
first off do you know how to create your own variables?
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming