0

Okay I've just started learning c++ in college but I'm stuck on a bit of what to do to get the program to read from an file with floats.

I've got floats like 38.5 33.6 44.45 42.86.

I am told to read these floats ("temperatures") in the program. I start out the code this way.

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

int main()
{
ifstream absoluteval;
absoluteval.open("tempinput.txt");
string w;
getline(absoluteval,w);
}

This is what I got so far but when I made an cout to check and see if it shows up correctly for these it comes up with 0 x 0 in the command prompt.

3
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by John A
0

Depends what you want to do with them. Since they're seperated by spaces, you could simply read them directly from the stream into the variable like this:

float floatAmount;
while (absoluteval >> floatAmount) {
    cout << floatAmount << endl;
}

Of course, if you actually want to store the values for later, you'll need an array or something.

0

Lol, I didnt understand any of that on the other hand, Joe is like you tell him 0x00014302 And hes like yeah I know that!

Good Job joe Keep it up!

0

Lol, I didnt understand any of that on the other hand, Joe is like you tell him 0x00014302 And hes like yeah I know that!

Good Job joe Keep it up!

I wish.

No seriously, there's a lot of more knowledgeable people here on the forums with C/C++. I just pretend to be an expert; I don't even know assembly, let alone machine code. ;)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.