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

Dani AI

Generated

Good catch by and good prompt from — the bug here is an input-tokenisation issue, not math. Operator>> splits on whitespace, so a line that looks like "YYYYMMDDHHMM F76.4" will come into the program as two tokens. Attempting substrings or indexed constructors on the wrong token produces out_of_range errors.

A safer pattern is to read a full input line and then split/validate it, or explicitly read two tokens and validate lengths before slicing. After reading the initial count with operator>>, remember the trailing newline is still in the stream; use ignore or consume that newline before calling getline. Always check string::size() before taking substrings, and use std::stoi/std::stod (with try/catch or pre-check) to convert text to numbers.

Practical example: read a line with std::getline, wrap it in std::istringstream, extract timestamp and reading tokens, verify timestamp.size() >= 12 and reading.size() >= 2, then parse year/month/day/hour/minute with substr+stoi. For temperature, use reading.front() for the scale and std::stod(reading.substr(1)) for the numeric value, converting Fahrenheit to Celsius when needed. Below is a compact template that follows this approach.

#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include <limits>

double f_to_c(double f) { return (f - 32.0) * 5.0 / 9.0; }

/* see text above for usage pattern: read count with >>, ignore newline,
   then getline each data line, parse with istringstream, validate sizes,
   use stoi/stod, handle exceptions as needed. */

Troubleshooting notes: print raw input when things fail, watch for extra blank lines, validate token lengths before indexing, and handle std::invalid_argument/std::out_of_range from stoi/stod. If portability is required for date parsing, consider platform routines like strptime (POSIX) or a small manual parser as shown.

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.