Sorry about the title, but I thought it would be interesting.
Here is my problem. I am supposed to convert a certain input into a desired output. The input is
2
201101190930 F76.4
201101191330 C16.3

The first number is the number of strings in the file. It then goes
YYYYMMDDHHMM
Year, month, day, hour, minutes, xtemp where x is F/C and the other is the temp.


This is supposed to be converted like so
24.67 C -- recorded on 01/19/2011 at 09:30
16.30 C -- recorded on 01/19/2011 at 13:30

I believe I know how to convert Fahrenheit to Celsius with a different function.
My problem is that I don't know how to make my string read the F76.4. I can read the other part and get that output, but when I implement the same string reading for the temperature, it says that my std is out of bounds.
Below is the code

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
     ...//the loop, declaration of input and the such
     cin >> input;
     string year(input, 0, 4);
     string month(input 4, 2);
     string day(input, 6, 2);
     string hours(input, 8, 2);
     string minutes(input, 10, 2);
     string x(input, 13, 1);
     string temp(input, 14, 2)

     cout << x << temp;
     cout << "-- recorded on " << month;
     cout << "/" << day << "/" << year;
     cout << " at " << hours << ":" << minutes;
     cout << endl;
return 0;
}

I know you can just give me the answer, but I'd prefer if you can explain it to me. I don't want to cheat. This is my major and I have a passion for it, but I can not find this answer in my book. Please help!

Thank you :D

Recommended Answers

All 4 Replies

Print out your "input" and see if its correctly formatted. Make sure you account for the spaces. Have you learned about classes and structures?

>>

I don't want to cheat. This is my major and I have a passion for it, but I can not find this answer in my book

Give you respect for this. Not many new posters have this mentality.

To me, structures would just make it look nicer. I do not know about classes though, so tomorrow I'll open that section and start studying it.

The input is correct because the teacher sent it to us. That is what really troubles me.

I have to go to bed now to wake up early, but I'll ask the teacher if he can help me tomorrow. If I can find the answer, I'll post it here so that it can be a reference to anyone with the problem. Thank you for your post :D

If I do get the answer, hopefully an admin will see it and will tag it correctly for others to find it. I know I couldn't find this in the search :)

Have a great night!
~Rimo

The teacher didn't help me, but I eventually found the problem.
Because of the white space between the numbers and the letter (F or C), it counted the writing as 2 strings. What that meant was that when it read F76.4, it tried to do the entire loop on just those few characters, and was "Out of bounds".
The code that does work looks like this:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
string filename;
cout << "Enter a data file name:";
cin >> filename;


double count;
cin >> count;

for (int i; i<count; i++)
{
        string s1,s2;
        cin >> s1;
        cin >> s2;
        string year(s1, 0, 4);
        string month(s1, 4, 2);
        string day(s1, 6, 2);
        string hours(s1, 8, 2);
        string minutes(s1, 10, 2);
        string x(s2, 0, 1);
        string temp(s2, 1, 4);

        cout << temp << " " << x << "--recorded on " << month;
        cout << "/" << day << "/" << year;
        cout << " at " << hours << ":" << minutes;
        cout << endl;
}
return 0;
}

That is the answer! Hopefully this can help others for later reference.

Make sure you account for the spaces

I now see what you are saying.
Thank you for the advice!

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.