I'm not understanding something about my program

This is a discussion on I'm not understanding something about my program within the C++ Programming forums, part of the General Programming Boards category; EDIT: I didn't read the "READ BEFORE POSTING" topic b/c I'm dumb. I just tried to edit it to put ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    7

    I'm not understanding something about my program

    EDIT: I didn't read the "READ BEFORE POSTING" topic b/c I'm dumb. I just tried to edit it to put code tags but I couldn't figure out how. Sorry..... :-(



    I'm having a problem with my program and I don't know what the problem is. I searched the internet after all my friends in my class were unavailable and hope this can help.

    In my program, I'm computing what the max and min values of the input table are. The table has 4 columns consisting of Time, Temp, Pressure, and Volume. The max and mins are the values for each T,P, and V at whatever time it is at. Becuase I can't post brackets for my if statements in a post here, I'm replacing them with \ and / respectively.

    The problem with this program is that the variables for my min and max values are not working out. When I go to put them in the last cout statements (I just wrote timemint as the last line to test it) it says error C2065: 'timemint' : undeclared identifier and I don't know why.





    ifstream in("homework4data.txt");
    ofstream outFile("HW5.txt");

    outFile << "TIME " << setw(23) << " TEMPERATURE " << setw(13) << " PRESSURE " << setw(13) << " VOLUME" << endl;
    cout << "TIME " << setw(23) << " TEMPERATURE " << setw(13) << " PRESSURE " << setw(13) << " VOLUME" << endl;
    outFile << "================================================= =====" << endl;
    cout << "================================================= =====" << endl;

    int mTime;

    double t,p,v,SumT = 0, SumP = 0,SumV = 0;
    double CountT = 0, CountP = 0, CountV = 0;

    outFile << setiosflags(ios::fixed) << setprecision(2);
    cout << setiosflags(ios::fixed) << setprecision(2);

    while ( ! in.eof() ) \
    in >> mTime >> t >> p >> v;
    if (!in.fail()) \
    int h,m;
    bool pm;
    pm = mil2civTime(mTime,h,m);

    if (pm)
    cout << setw(2) << setfill(' ') << h << ':' << setw(2) << setfill('0') << m << " P.M." << '\t' << t << '\t' << '\t' << p << '\t' << '\t' << v << endl;
    else
    cout << setw(2) << setfill(' ') << h << ':' << setw(2) << setfill('0') << m << " A.M." << '\t' << t << '\t' << '\t' << p << '\t' << '\t' << v << endl;

    if (pm)
    outFile << setw(2) << setfill(' ') << h << ':' << setw(2) << setfill('0') << m << " P.M." << '\t' << t << '\t' << '\t' << p << '\t' << '\t' << v << endl;
    else
    outFile << setw(2) << setfill(' ') << h << ':' << setw(2) << setfill('0') << m << " A.M." << '\t' << t << '\t' << '\t' << p << '\t' << '\t' << v << endl;

    double count = 0;
    double mint,minv,minp,maxt,maxv,maxp;
    int timemint,timeminv,timeminp,timemaxt,timemaxv,timem axp;
    if (count == 1 || t < mint) \
    mint = t;
    timemint = mTime;
    /
    if (count == 1 || p < minp) \
    minp = p;
    timeminp = mTime;
    /
    if (count == 1 || v < minv) \
    minv = v;
    timeminv = mTime;
    /
    if (count == 1 || t > maxv) \
    maxt = t;
    timemaxt = mTime;
    /
    if (count == 1 || p > maxp) \
    maxp = p;
    timemaxp = mTime;
    /
    if (count == 1 || v > maxv) \
    maxv = v;
    timemaxv = mTime;
    /
    SumT += t;
    CountT += 1;

    SumP += p;
    CountP += 1;

    SumV += v;
    CountV += 1;

    /
    /

    outFile << "================================================= =====" << endl;
    cout << "================================================= =====" << endl;
    outFile << "AVERAGE" << '\t' << '\t' << SumT/CountT << '\t' << '\t' << SumP/CountP << '\t' << '\t' << SumV/CountV << endl;
    cout << "AVERAGE" << '\t' << '\t' << SumT/CountT << '\t' << '\t' << SumP/CountP << '\t' << '\t' << SumV/CountV << endl;
    cout << "================================================= =====" << endl;
    cout << "STANDARD" << endl;
    cout << '\t' << '\t' << "17.31" << '\t' << '\t' << ".3627" << '\t' << '\t' << "67.234" << endl;
    cout << "DEVIATION" << endl;
    cout << "================================================= =====" << endl;
    cout << '\t' << '\t' << "MAXIMUM" << '\t' << '\t' << '\t' << '\t' << "MINIMUM" << endl;
    cout << "Temperature" << '\t' << "???.?? @ ??:?? ?? ??.?? @ ??:?? ??" << endl;
    cout << "Pressure" << '\t' << "???.?? @ ??:?? ?? ??.?? @ ??:?? ??" << endl;
    cout << "Volume" << '\t' << '\t' << "???.?? @ ??:?? ?? ??.?? @ ??:?? ??" << endl;
    cout << timemint << endl;
    Last edited by F5 Tornado; 10-24-2007 at 08:03 PM. Reason: I'm dumb

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    sorry, but can you please edit it and post all of your code. when you do it click the code button in the editor then within the [CODE ][/CODE] blocks paste your entire code. this way we can compile it ourselves to see the compile error.

    from looking at it it looks like the variable is declared fine. my only guess is that timemint is out of scope (ie declared in functionA and your trying to use it in functionB).
    edit: ill try and give a quick example.
    Code:
    void functionA()
    {
        int timemint = 0; // variable only accessible in this function
    }
    
    void functionB()
    {
      {
          int timemint = 5; // variable only accessible within the braces
       }
        cout << timemint; // timemint is out of scope, ie not declared within the current code block and is not 'global'--error
    }
    also, do you have 2 if statements back-to-back checking the same thing (if (pm))? is that necessary?

    edit x2: its hard to tell, but i think it is out of scope, because it looks like its declared in the while code block, thus can only be accessed within that block. declare it outside and before the while block.
    Last edited by nadroj; 10-24-2007 at 08:31 PM.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    Code:
    // HW-5.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cmath>
    
    using namespace std;
    
    bool mil2civTime( int mTime, int& hr, int& min) {
    	hr = mTime /100;
    	min = mTime % 100;
    	if (hr == 0) hr = 12;
    	if (hr > 12) hr -= 12;
    	return mTime >= 1200;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	ifstream in("homework4data.txt");
    	ofstream outFile("HW5.txt");
    
    	outFile << "TIME " << setw(23) << " TEMPERATURE " << setw(13) << " PRESSURE " << setw(13) << " VOLUME" << endl;
    	cout << "TIME " << setw(23) << " TEMPERATURE " << setw(13) << " PRESSURE " << setw(13) << " VOLUME" << endl;
    	outFile << "======================================================" << endl;
    	cout << "======================================================" << endl;
    	
    	int mTime;
    	
    	double t,p,v,SumT = 0, SumP = 0,SumV = 0;
    	double CountT = 0, CountP = 0, CountV = 0;
    			
    	outFile << setiosflags(ios::fixed) << setprecision(2);	
    	cout << setiosflags(ios::fixed) << setprecision(2);	
    	
    	while ( ! in.eof() ) {
    		in >> mTime >> t >> p >> v;
    		if (!in.fail()) {
    			int h,m;
    			bool pm;
    			pm = mil2civTime(mTime,h,m);
    
    			if (pm) 
    				cout << setw(2) << setfill(' ') << h << ':' << setw(2) << setfill('0') << m << " P.M." << '\t' << t << '\t' << '\t' << p << '\t' << '\t' << v << endl;
    			else
    				cout << setw(2) << setfill(' ') << h << ':' << setw(2) << setfill('0') << m << " A.M." << '\t' << t << '\t' << '\t' << p << '\t' << '\t' << v << endl;
    
    			if (pm) 
    				outFile << setw(2) << setfill(' ') << h << ':' << setw(2) << setfill('0') << m << " P.M." << '\t' << t << '\t' << '\t' << p << '\t' << '\t' << v << endl;
    			else
    				outFile << setw(2) << setfill(' ') << h << ':' << setw(2) << setfill('0') << m << " A.M." << '\t' << t << '\t' << '\t' << p << '\t' << '\t' << v << endl;
    		
    			
    			SumT += t;
    			CountT += 1;
    			
    			SumP += p;
    			CountP += 1;
    			
    			SumV += v;
    			CountV += 1;
    		
    			double count = 0;
    			double mint,minv,minp;
    			double maxt,maxv,maxp;
    			
    			if (count == 1 || t < mint) {
    				int timemint;
    					mint = t;
    					timemint = mTime;
    			}
    			if (count == 1 || p < minp) {
    				int timeminp;
    					minp = p;
    					timeminp = mTime;
    			}
    			if (count == 1 || v < minv) {
    				int timeminv;
    					minv = v;
    					timeminv = mTime;
    			}
    			if (count == 1 || t > maxt) {
    				int timemaxt;
    					maxt = t;
    					timemaxt = mTime;
    			}
    			if (count == 1 || p > maxp) {
    				int timemaxp;
    					maxp = p;
    					timemaxp = mTime;
    			}
    			if (count == 1 || v > maxv) {
    				int timemaxv;
    					maxv = v;
    					timemaxv = mTime;
    			}
    			
    		}
    	}
    
    			
    	
    	outFile << "======================================================" << endl;
    	cout << "======================================================" << endl;
    	outFile << "AVERAGE" << '\t' << '\t' << SumT/CountT << '\t' << '\t' << SumP/CountP << '\t' << '\t' << SumV/CountV << endl;
    	cout << "AVERAGE" << '\t' << '\t' << SumT/CountT << '\t' << '\t' << SumP/CountP << '\t' << '\t' << SumV/CountV << endl;
    	cout << "======================================================" << endl;
    	cout << "STANDARD" << endl;
    	cout << '\t' << '\t' << "17.31" << '\t' << '\t' << ".3627" << '\t' << '\t' << "67.234" << endl;
    	cout << "DEVIATION" << endl;
    	cout << "======================================================" << endl;
    	cout << '\t' << '\t' << "MAXIMUM" << '\t' << '\t' << '\t' << '\t' << "MINIMUM" << endl;
    	cout << "Temperature" << '\t' << "???.?? @ ??:?? ??        ??.?? @ ??:?? ??" << endl;
    	cout << "Pressure" << '\t' << "???.?? @ ??:?? ??          ??.?? @ ??:?? ??" << endl;
    	cout << "Volume" << '\t' << '\t' << "???.?? @ ??:?? ??          ??.?? @ ??:?? ??" << endl;
    	cout << timemint << endl;
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    I also just realized that you may need the text file but I dont' know how to attach that. This is directly copied from it. first column is military time, next is temp, then pressure, then volume.

    0845 34.2 32.2 101.5
    0922 38.8 32.2 112.1
    1047 44.8 32.4 142.5
    1159 51.3 32.0 152.0
    1215 55.5 31.8 166.5
    1329 61.3 31.9 186.5
    1441 67.5 32.3 208.0
    1523 72.1 32.8 226.2
    1656 76.8 32.5 256.4
    1738 83.5 32.7 286.9
    1800 88.9 33.0 318.6

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    Yet another case of me being dumb. Found the attachment button.
    Attached Files Attached Files

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    For something that's so small, it's better to post it directly. It takes less time for readers to see the information. An attachment slows them down, but it's worth it if the data is, say, ten thousand lines long.

    Code:
    	while ( ! in.eof() ) {
    		in >> mTime >> t >> p >> v;
    That's probably not a good idea. http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    But it's simple enough to go:
    Code:
    while(in >> mTime >> t >> p >> v) {
    Code:
    			if (count == 1 || t < mint) {
    				int timemint;
    					mint = t;
    					timemint = mTime;
    			}
    			if (count == 1 || p < minp) {
    				int timeminp;
    					minp = p;
    					timeminp = mTime;
    			}
    			if (count == 1 || v < minv) {
    				int timeminv;
    					minv = v;
    					timeminv = mTime;
    			}
    			if (count == 1 || t > maxt) {
    				int timemaxt;
    					maxt = t;
    					timemaxt = mTime;
    			}
    			if (count == 1 || p > maxp) {
    				int timemaxp;
    					maxp = p;
    					timemaxp = mTime;
    			}
    			if (count == 1 || v > maxv) {
    				int timemaxv;
    					maxv = v;
    					timemaxv = mTime;
    			}
    Why don't you wrap all of that inside something like this, so that you can get rid of all of those references to count?
    Code:
    if(count == 1) {
    
    }
    Creating a function to do this would make the code even more concise.

    Code:
    CountT += 1;
    Same as CountT ++. Use whichever you find more readable.

    The if(pm) and else branches do the same thing, on two occasions.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    			if (count == 1 || t < mint) {
    				int timemint;
    					mint = t;
    					timemint = mTime;
    			}
    as i suspected earlier, your variable timemint is declared only for the scope of that one if statement. it can not be accessed and does no longer exist, at the bottom of your program outside the block it was declared in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21