Week 3 and still have problems

This is a discussion on Week 3 and still have problems within the C++ Programming forums, part of the General Programming Boards category; I just made a program to calculate the distance to a rainbow...it seemed to work. ..but the Prof wants that ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    73

    Week 3 and still have problems

    I just made a program to calculate the distance to a rainbow...it seemed to work. ..but the Prof wants that version which I think is callled "Interactive" and he wants an "Noninteractive" version. That would be be with InData.txt files. Well I did that and when I went to test the program it just said "Press any key to continue" thats it. I checked the files and there was an Output.txt file with the answers..is this what this should do? Only show the outpt.txt file vs. showing on the comptuer screen. I hope this is right, please let me know. thanks

    Bryan

    Here is my Noninteractive version...Please let me know if this is built correctly.

    Code:
    //*****************************************
    //How tall is a rainbow
    //This program finds out how tall a 
    //rainbow is with the distance of the 
    //rainbow already defined by the programmer
    //*****************************************
    
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cmath>
    
    using namespace std;
    
    	float PI;
    	float MagicAngle;
    	float RadiansMa;
    	float TangentMa;
    	float RainbowDistance;
    	float RainbowHeight;
    	ifstream InData;
    	ofstream OutData;
    
    int main()
    
    {
    	OutData << fixed <<showpoint;
    	InData.open("InData.txt");
    	OutData.open("OutData.txt");
    
    	const double PI = 3.14159265;
    	const double  MagicAngle = 42.3333333;
    	
    	InData >> RainbowDistance;
    	
    	RadiansMa = MagicAngle * PI/180;
    	TangentMa = tan(RadiansMa);
    	RainbowHeight = TangentMa *  RainbowDistance;
    
    	OutData << setprecision(4);
    	OutData << "Distance is " <<RainbowDistance << endl
    		    << "Height of rainbow is " <<RainbowHeight <<endl;
    
    	return 0;
    
    }
    Last edited by romeoz; 06-21-2003 at 06:25 PM.

  2. #2
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    Yes, it should write everything to the file, if you want to display it on the screen as well, you'll have to cout it so it'll appear on the console. Also, its not a good practice to get into to declare all of your variables globally (outside of main).

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Sure, i'd say a "non-interactive" version of the program would entail no input directly from the user (ie. Input from data file)
    Why not ask him/her?

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    I would but the assignemt is due by midnight tonight. So its either ask people from here or turn it in and hope I did it right. This is my first programming class and I get confused very easily. thanks for helping out though.

    Bryan

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    Let me make sure I know what you are saying. You say I should declare all my "floats" after int main() and maybe declare my const double above int main()? I just entered cout and it showed up on the screen as well as making an output.txt. :-)

    Thanks

    Bryan
    Last edited by romeoz; 06-21-2003 at 04:30 PM.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    romeoz: Please read this thread before posting code
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    I did not undersatng what code did but after I added that to the above it look so much nicer:-) I will remember that for next time I post..thanks

    Bryan

  8. #8
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by romeoz
    Let me make sure I know what you are saying. You say I should declare all my "floats" after int main() and maybe declare my const double above int main()? I just entered cout and it showed up on the screen as well as making an output.txt. :-)

    Thanks

    Bryan
    Yes; declare your consts outside main(), and declare your local variables (ie. all your float variables) inside main()
    Also, you've declared PI and MagicAngle twice. You're getting away with this because those with useful values were declared last. If you're going to take the above suggestion, you'll need to remove the non-const declarations of PI and MagicAngle.
    Code:
    //*****************************************
    //How tall is a rainbow
    //This program finds out how tall a
    //rainbow is with the distance of the
    //rainbow already defined by the programmer
    //*****************************************
    
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cmath>
    
    using namespace std;
    
    const double PI = 3.14159265;
    const double MagicAngle = 42.3333333;
    
    int main()
    {
    	float RadiansMa;
    	float TangentMa;
    	float RainbowDistance;
    	float RainbowHeight;
    
    	ifstream InData("InData.txt");
    	ofstream OutData("OutData.txt");
    
    	OutData << fixed << showpoint;
    	InData >> RainbowDistance;
    
    	RadiansMa = MagicAngle * PI/180;
    	TangentMa = tan(RadiansMa);
    	RainbowHeight = TangentMa * RainbowDistance;
    
    	OutData << setprecision(4);
    	OutData << "Distance is " << RainbowDistance << endl
    		<< "Height of rainbow is " << RainbowHeight <<endl;
    
    	return 0;
    
    }

Popular pages Recent additions subscribe to a feed

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