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.

Recommended Answers

All 3 Replies

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.

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!

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. ;)

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.