Hi, I'm learning intro to c++.

My question is, how do I input values that come from a different text file so that the program uses those values for calculations?

Basically, it gives me a list of numbers and tells me what the numbers are for... and then I have to integrate them using ifstream.

For example..

Text file:
111
222
333
444


555
666

and each of those lines represent a variable,

a
b
c
d


e
f

(notice the space in between d and e, does it change anything?)
I got so far as to opening the file correctly... but I'm stuck.

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;


int main() {


char myFileName[64];
double a = 0;
double b = 0;
double c = 0;
double d = 0;
double e = 0;
double f = 0;
ifstream locationtemp;


cout << "Enter the file name.\n";
cin >> myFileName;


locationtemp.open(myFileName);


if ( locationtemp.fail() )
{
cerr << "\n\nERROR:Unable to open file/File did not open correctly\n";
return(1);
}

btw those arent the real values or variables.

Any help is greatly appreciated. Thanks :)

What is numberObs?
And I don't get what do you want exactly?

oops sorry it's supposed to be locationtemp :S

my question was how do I use values given in a text document (the "myFileName") and input them into my program variables so they can be used for calculations?

Just use locationtemp as you would use cin.
Of course, that is if your file contains only numbers.

yes it does contain only numbers. so i would have to make a new variable? for example

int number = 0;
(...)

locationtemp >> number;

?

but how would I know which variables each number will be assigned to? am i able to specify it using 'line' or something?

The file will be read line by line. So the first line would be stored in the first variable. Ofcourse, if you want to make it possible to loop throught the file until all the variables are read in then your best bet would be to use and array (not stricktly true, vectors would be better but don't worry about those yet).

so create an array of doubles then loop throught the file storing each line into the next cell in the array.

i would do something like this

#include <iostream>
#include <string>
#include <cstdlib>
#include <ifstream>

int main(void){
    char myFileName[64];
    string line;
    double numbers[100] = {0};
    int x = 0;
    
    ifstream locationtemp;
    
    cout << "Enter the file name.\n";
    cin >> myFileName;
    
    
    locationtemp.open(myFileName);
    if ( !locationtemp.good() ){
       cerr << "\n\nERROR:Unable to open file/File did not open correctly\n";
       cin.get();
       return(1);
       }
    
    while( !locationtemp.eof() ){
           getline(locationtemp, line);
           numbers[x++] = strtod(line);
           }
    locationtemp.close();
    return 0;
}

Something along those lines. Untested i don't have access to compilers in college

Chris

The while loop won't work properly, eof is unsafe to use in looping.
This is better:

while (getline(locationtemp, line)){
         numbers[x++] = strtod(line);
}
commented: Thanks, always wanting to know new things about stability +1

ah thanks for pointing that out, i was forgetting there are characters that can cause eof to return true!

Chris

ah thanks for pointing that out, i was forgetting there are characters that can cause eof to return true!

Chris

Furthermore, even if you reach end of file, eof won't be set to true until you try to read after the EOF. That will cause this kind of error:
FILE.txt
My
name
is
//end

//screen output
My
name
is
is
//end

Thanks for all the help.
I'm still confused though, is there a good site that explains the meaning of codes well for beginners?

I still don't get how the values get assigned in order. Will I have to list the variables in order at the beginning?
__________

int main () {

char filename[64];
int presentTemp = 0; // first number from text
double desiredTemp = 0; // second
double timeInterval = 0; // third
double toleranceOn = 0; // fourth
double toleranceOff = 0; // fifth

double changeInTemp = 0; // from sixth value onwards (to the end of file)

ifstream ccLocation;

cout << "Enter the file name.\n";
cin >> myFileName;

ccLocation.open(filename);
if ( ccLocation.fail() ){
cerr << "\n\nERROR:Unable to open file\n";
cin.get();
return(1);
}

while( getline(ccLocation , line) ){
getline(ccLocation, line);
numbers[x++] = strtod(line);
}

ccLocation.close();

__________

would that be correct?

I would advise you to look at more basic things first, you are jumping ahead.
File manipulation is learned well after loops, arrays and string manipulations... it's not without reason.

From the code i originally posted, all you had to do was update the loop to how sci@phy pointed out.

Then understand it....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.