The GAT Thread

A place for members to speak about almost anything, at a reasonable level.

Moderator:Lexx Yovel

Locked
User avatar
Apium
Colonel
Posts:2568
Joined:Sat Mar 25, 2006 8:41 pm
Location:North Carolina

Post by Apium » Mon May 22, 2006 8:41 pm

Kurke_Aumea wrote:
Apium wrote:Actually looking at that now, I realize that I should have put notes in there so I would know what the fuck I was doing when I looked back at it.


Ahhh, the ever lovely comments. Still, try using indents to help distinguish your if and case statements. It will help you when you're looking for things later. OF course, you probably did use indents, they just didn't make the conversion on the forums. On that note, try using the Code feature when posting coding, it really helps. It is up beside the bold, italics, link, and image tags when you make a post.

What makes programming so messed up is how complex it is to do simple things. For instance, the program you posted is realtively simple, but takes a lot of coding to get it done. That's probably why I hate programming most. Still, if you can understand step-by-step processes, if-else statements, and a few other logical processes programming can be pretty simple to understand.

And yes, I can understand everything in that program... Wow, that means I actually paid attention in programming class...

Still, I am slightly confused by how you are naming your variables. In the one else statement you created two doubles, answer2 and answer25. I'm just curious why you labeled it answer25? Still, quite effective use of an if-else statement and a case statement.


There were indents in such in the actual code it just didnt transfer when i copied and pasted it. It took me 5min to figure out why I had those 2 variables for that else statement. It wasn't giving me the right answers when I used this equation:
Kelvin = ((Fahrenheit - 32) * (5/9)) + 273
so I broke it down.
First I converted it to celsius and stored that in the variable answer2.
Then I converted celsius to kelvin by simply adding 273 which was stored in answer25. The 25 is like 2.5 which is why i named it like that.

This program caused me a lot of grief not b/c of complexity but b/c of the math lol. I was quite proud of this program it was my first one in C++ and i was still 15 at the time. I think I said latest earlier its not its my first for C++. I was thinking of something else.
swtor: Aeroxis

Apium Auvair- Master Medic
Kalice- Master Architect
Aurelie Auvair- Jedi Master
Victoire Auvair- Master Armorsmith

User avatar
Apium
Colonel
Posts:2568
Joined:Sat Mar 25, 2006 8:41 pm
Location:North Carolina

Post by Apium » Mon May 22, 2006 8:45 pm

Kurke_Aumea wrote:
Apium wrote:
Ri'Me wrote:ok, I have no clue what any of that is, probably take me 45 minutes to an hour just to type all that too.. but I will learn


lol if you want to learn programming especially by yourself start with basic. That was C++. Basic is basically basic, pun totally intended. Anyways you can learn all the concepts with out overwhelming yourself.


Yeah, basic and C++ are two different animals, but both use the same logic. The nice thing about Visual Basic is that you can make a visual user interface to see what your programming corresponds to. With C++, you get that old school DOS prompt and text output. The problem is that since they are two different animals, learning one probably won't help you code the other. IT will make you understand the logic behind it all though. Then again, it all depends how you learn it too. I went through a C++ class at both DeVry and YSU. I had a devil of a time at DeVry because of how the professor taught it. At YSU, it was mostly a breeze because of how it was presented to me.


I doubt I would have figured out C++ without learning BASIC first. I still have plenty to learn like I'm sure a professional programmer could take my earlier program which is 441kb and make it under 300kb. Then again I took a program that was 500kb and made it 450kb with 10x the features.
swtor: Aeroxis

Apium Auvair- Master Medic
Kalice- Master Architect
Aurelie Auvair- Jedi Master
Victoire Auvair- Master Armorsmith

User avatar
Kurke_Aumea
Colonel
Posts:3281
Joined:Sun Jun 27, 2004 3:11 pm
Contact:

Post by Kurke_Aumea » Mon May 22, 2006 9:12 pm

Once you learn how to use other C++ features, you'll figure out how to really condense your programming. Writing seperate functions outside of function main() will really allow you to make more simplified functions. Instead of stuffing everything in function main(), you can put all your calculations in their own functions. Not only that, plublic variables become your friend.

I need to go dig up my gas pump problem from class last semester and see if you can't figure out what is going on (hmmm... should I leave the comments in or take them out...). Basically, the program I wrote simulated a gas pump. You have three grades of fuel at a set price. You then input how many gallons of which fuel type you put into the car. The program then calculates the price and creates running totals for each type of fuel and a grand total. It seems really simple, but the nested case statements can really confuse you if you're not used to them.

Time to go digging...

Oh yeah, here is how you use the code feature on the forums:

Code: Select all

Int main()
{
    int number = 0;

    cout << "The value of the variable number is "
         << number
         << endl;

    return 0;
}
Image

User avatar
Waucod Meesman
Village Idiot
Posts:2222
Joined:Thu May 26, 2005 11:19 am
Location:Jacksonville area
Contact:

Post by Waucod Meesman » Mon May 22, 2006 10:18 pm

Not that anyone care but I maxed my math on the FCAT (Florida Comprhensive Assesment Test) and I got a 5 in reading and I almost maxed it too (5's are the highest you can get just ask anyone who lives in Florida about the FCAT its a big thing)
Image

User avatar
Apium
Colonel
Posts:2568
Joined:Sat Mar 25, 2006 8:41 pm
Location:North Carolina

Post by Apium » Tue May 23, 2006 6:04 am

Kurke_Aumea wrote:Once you learn how to use other C++ features, you'll figure out how to really condense your programming. Writing seperate functions outside of function main() will really allow you to make more simplified functions. Instead of stuffing everything in function main(), you can put all your calculations in their own functions. Not only that, plublic variables become your friend.

I need to go dig up my gas pump problem from class last semester and see if you can't figure out what is going on (hmmm... should I leave the comments in or take them out...). Basically, the program I wrote simulated a gas pump. You have three grades of fuel at a set price. You then input how many gallons of which fuel type you put into the car. The program then calculates the price and creates running totals for each type of fuel and a grand total. It seems really simple, but the nested case statements can really confuse you if you're not used to them.

Time to go digging...

Oh yeah, here is how you use the code feature on the forums:

Code: Select all

Int main()
{
    int number = 0;

    cout << "The value of the variable number is "
         << number
         << endl;

    return 0;
}


I'd like a look at it if you can find it. Take the comments out, I want a challenge.
swtor: Aeroxis

Apium Auvair- Master Medic
Kalice- Master Architect
Aurelie Auvair- Jedi Master
Victoire Auvair- Master Armorsmith

User avatar
Kurke_Aumea
Colonel
Posts:3281
Joined:Sun Jun 27, 2004 3:11 pm
Contact:

Post by Kurke_Aumea » Tue May 23, 2006 6:43 pm

Ask and ye shall receive..

Note: This program only uses function main() as we made this only about halfway through the course. In retrospect, this actually seems pretty easy. I need to go find some of the more difficult ones we did. Oh yeah, some of the formatting didn't quite make it in the copy.

Code: Select all

/***********************************************************************/
/*  File name: assignment05.cc                                         */
/*  Author: Kurt                                                       */
/*  Date: March 1, 2006                                                */
/*  Description: This program will simulate a pricing mechanism of a   */
/*               gasoline pump.  It will prompt a user for a gasoline  */
/*               grade and for a quantity.  It will then display the   */
/*               totals for the transaction.  When the program ends,   */
/*               it will display totals for all grades of gasoline and */
/*               a grand total.                                        */
/***********************************************************************/

#include <iostream>
#include <iomanip>
using namespace std;

const float ECONOMY = 2.199;
const float REGULAR = 2.299;
const float PREMIUM = 2.399;

/***************************function main()*****************************/
int main()
{

    char grade = ' ';
    float qty = 0;
    float grandE = 0;
    float grandR = 0;
    float grandP = 0;
    float qtyE = 0;
    float qtyR = 0;
    float qtyP = 0;

    grade = ' ';

    while (grade != '*')
    {
        grade = ' ';

   while (grade != '*' && grade != 'e' && grade != 'E' && grade != 'r'
       && grade != 'R' && grade != 'p' && grade != 'P')
        {
       cout << "\nSelect a grade from the list by entering"
            << "\na letter or enter * to end the program."
           << endl
           << endl;
       cout << "Grades of Gasoline\n"
           << "------------------\n"
           << "Economy = E\n"
           << "Regular = R\n"
           << "Premium = P" << endl
           << endl;
       cout << "Choose your grade: ";
       cin  >> grade;
        }
   
   if (grade == '*')
   {
       continue;
   }

   qty = 0;

   while (qty <= 0)
   {
       cout << "\nPlease enter a quantity (in gallons) of gasoline: ";
       cin  >> qty;
       cout << endl;
   }

   cout << fixed << setprecision(2);

   switch (grade)
   {
       case 'E': case 'e':
      cout << setw(8) << " Grade  "
           << setw(5) << " Qty."
           << setw(7) << "  Price " << endl;

      cout << setw(8) << "------- "
           << setw(6) << "----- "
           << setw(7) << "-------" << endl;

      cout << setw(8) << "Economy "
           << setw(4) << qty << " "
           << setw(1) << "$"
           << setw(6) << ECONOMY * qty << endl
           << endl;
      break;
      
       case 'R': case 'r':
      cout << setw(8) << " Grade  "
           << setw(5) << " Qty."
           << setw(7) << "  Price "
           << endl;

      cout << setw(8) << "------- "
           << setw(6) << "----- "
           << setw(7) << "-------"
           << endl;

      cout << setw(8) << "Regular "
           << setw(4) << qty << " "
           << setw(1) << "$"
           << setw(6) << REGULAR * qty << endl
           << endl;
      break;

       case 'P': case 'p':
      cout << setw(8) << " Grade  "
           << setw(5) << " Qty."
           << setw(7) << "  Price "
           << endl;

      cout << setw(8) << "------- "
           << setw(6) << "----- "
           << setw(7) << "-------"
           << endl;

      cout << setw(8) << "Premium "
           << setw(4) << qty << " "
           << setw(1) << "$"
           << setw(6) << PREMIUM * qty
           << endl
           << endl;
      break;
   }

   if (grade == 'E' || grade == 'e')
   {
       grandE = grandE + (ECONOMY * qty);
       qtyE = qtyE + qty;
   }
   else if (grade == 'R' || grade == 'r')
   {
       grandR = grandR + (REGULAR * qty);
       qtyR = qtyR + qty;
   }
   else if (grade == 'P' || grade == 'p')
   {
       grandP = grandP + (PREMIUM * qty);
       qtyP = qtyP + qty;
   }

   cout << "-------------------------------------------------------"
        << endl;
    }

    cout << "\n-------------------------------------------------------"
        << endl;

    cout << endl << "The grand totals for each grade are as follows:"
        << endl
        << endl;

    cout << setw(8) << " Grade  "
        << setw(8) << "  Qty.   "
        << setw(9) << "  Price   "
        << endl;

    cout << setw(8) << "------- "
        << setw(8) << "-------- "
        << setw(9) << "---------"
        << endl;

    cout << setw(8) << "Economy "
        << setw(8) << qtyE
        << setw(2) << "$"
        << setw(8) << grandE
        << endl;

    cout << setw(8) << "Regular "
        << setw(8) << qtyR
        << setw(2) << "$"
        << setw(8) << grandR
        << endl;

    cout << setw(8) << "Premium "
        << setw(8) << qtyP
        << setw(2) << "$"
        << setw(8) << grandP
        << endl;
   
    cout << setw(8) << "------- "
        << setw(8) << "-------- "
        << setw(9) << "---------"
        << endl;
   
    cout << setw(8) << "Total   "
        << setw(8) << qtyE + qtyR + qtyP
        << setw(2) << "$"
        << setw(8) << grandE + grandR + grandP << endl
        << endl;
       
    cout << "-------------------------------------------------------"
        << endl
        << endl;

    return 0;
}


Feel free to compile it in a C++ compiler. It was all coded on a Unix machine, so I don't know how a Windows compiler would work on it.

Here's the .exe file. I zipped it, so feel free to run a virus scan on it jsut to be sure.

http://www.geocities.com/yoda101705/ass ... mments.zip

Note: If you run the .exe and you end the program with a * like you're supposed to, the program will close itself and you won't be able to see the final totals. Run it from an open command prompt and that should eliminate the problem.
Image

User avatar
Kurke_Aumea
Colonel
Posts:3281
Joined:Sun Jun 27, 2004 3:11 pm
Contact:

Post by Kurke_Aumea » Tue May 23, 2006 7:03 pm

The above was our initial gas pump problem. We expanded it later with additional functions and whatnot. Let me know and I will post the coding and some executables.
Image

User avatar
Apium
Colonel
Posts:2568
Joined:Sat Mar 25, 2006 8:41 pm
Location:North Carolina

Post by Apium » Tue May 23, 2006 7:05 pm

I wiped my computer clean yesterday so I don't have my compiler right now. I get the idea though. Its more advanced than my program would have been. I could have done the same thing add about 20 lines of code though.

There are a couple questions I have:
What are the setw(a number)??
What is this line "cout << fixed << setprecision(2);"???

Thats it. I figure while we are on programming I might as well learn something.
swtor: Aeroxis

Apium Auvair- Master Medic
Kalice- Master Architect
Aurelie Auvair- Jedi Master
Victoire Auvair- Master Armorsmith

User avatar
Apium
Colonel
Posts:2568
Joined:Sat Mar 25, 2006 8:41 pm
Location:North Carolina

Post by Apium » Tue May 23, 2006 7:09 pm

Can you look through this and see what else I could have done to clean this up? It is complete program from earlier.

BTW I noticed you declared all your variables at the beginning of the program. Is that standard for most people or just personal for you?

Code: Select all

// Program to convert Celius, Fahrenheit, and Kelvin
// Fahrenheit = Celsius * (9/5) + 32
// Celsius = (Fahrenheit - 32) * (5/9)
// Kelvin = Celsius + 273
// Kelvin = ((Fahrenheit - 32) * (5/9)) + 273
// programmed by Tony Mantovani

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofARgs, char* pszArgs[])
{

int nfinish;

while(nfinish != 0)
{
int nVarible1;
cout << "1 for Fahrenheit to....\n2 for Celsius to....\n3 for Kelvin to....\nChoose 1,2, or 3 and hit enter to continue....";
cin  >> nVarible1;




switch(nVarible1)
{
    case 1:
        int nFahrenheit1;
        cout << "\n1 for Fahrenheit to Celsius\n2 for Fahrenheit to Kelvin...";
        cin  >> nFahrenheit1;
       
                if (nFahrenheit1 != 2)
                {
                     double dFtoC;
                     cout << "\nWhat is the fahrenheit degrees you would like to convert to celsius?...";
                     cin  >> dFtoC;
                     double answer1;
                     answer1 = (dFtoC - 32) * .5555555555555555555555555555555555555555555555555;
                     cout << "\nYour answer is:";
                     cout << answer1 << endl;
                     cout << "\n1 to continue or 0 to exit...";
                     cin  >> nfinish;
                   
                }
                else
                {
                     double dFtoK;
                     cout << "\nWhat is the fahrenheit degrees you would like to convert to kelvin?...";
                     cin >> dFtoK;
                     double answer2;
                     double answer25;
                     answer2 = (dFtoK - 32) * .5555555555555555555555555555555555555555555555555;
                     answer25 = answer2 + 273;
                     cout << "\nYour answer is:";
                     cout << answer25 << endl;
                     cout << "\n1 to continue or 0 to exit...";
                     cin  >> nfinish;
                }
        break;
   
    case 2:   
        int nCelsius1;
        cout << "\n1 for Celsius to Fahrenheit\n2 for Celsius to Kelvin...";
        cin  >> nCelsius1;
        if (nCelsius1 != 2)
                {
                     double dCtoF;
                     cout << "\nWhat is the celcius degrees you would like to convert to Fahrenheit?...";
                     cin  >> dCtoF;
                     double answer3;
                     answer3 = dCtoF * 1.8 + 32;
                     cout << "\nYour answer is:";
                     cout << answer3 << endl;
                     cout << "\n1 to continue or 0 to exit...";
                     cin  >> nfinish;
                   
                }
                else
                {
                     double dCtoK;
                     cout << "\nWhat is the Celsius degrees you would like to convert to kelvin?...";
                     cin >> dCtoK;
                     double answer4;
                     answer4 = dCtoK + 273;
                     cout << "\nYour answer is:";
                     cout << answer4 << endl;
                     cout << "\n1 to continue or 0 to exit...";
                     cin  >> nfinish;
                }
        break;
       
    case 3:
        int nKelvin1; 
        cout << "\n1 for Kelvin to Fahrenheit\n2 for Kelvin to Celsius...";
        cin  >> nKelvin1;
        if (nKelvin1 != 2)
                {
                     double dKtoF;
                     cout << "\nWhat is the kelvin you would like to convert to Fahrenheit?...";
                     cin  >> dKtoF;
                     double answer5;
                     double answer55;
                     answer5 = dKtoF - 273;
                     answer55 = answer5 * 1.8 + 32;
                     cout << "\nYour answer is:";
                     cout << answer55 << endl;
                     cout << "\n1 to continue or 0 to exit...";
                     cin  >> nfinish;
                   
                }
                else
                {
                     double dKtoC;
                     cout << "\nWhat is the kelvin you would like to convert to celsius?...";
                     cin >> dKtoC;
                     double answer6;
                     answer6 = dKtoC - 273;
                     cout << "\nYour answer is:";
                     cout << answer6 << endl;
                     cout << "\n1 to continue or 0 to exit...";
                     cin  >> nfinish;
                }
        break;
       
    default:
        cout << "\nYou didn't enter a 1, 2, or 3\n";
       
}
}       








system("PAUSE");
return 0;
}
swtor: Aeroxis

Apium Auvair- Master Medic
Kalice- Master Architect
Aurelie Auvair- Jedi Master
Victoire Auvair- Master Armorsmith

User avatar
Kurke_Aumea
Colonel
Posts:3281
Joined:Sun Jun 27, 2004 3:11 pm
Contact:

Post by Kurke_Aumea » Tue May 23, 2006 7:33 pm

Apium wrote:I wiped my computer clean yesterday so I don't have my compiler right now. I get the idea though. Its more advanced than my program would have been. I could have done the same thing add about 20 lines of code though.

There are a couple questions I have:
What are the setw(a number)??
What is this line "cout << fixed << setprecision(2);"???

Thats it. I figure while we are on programming I might as well learn something.


The setw(#) command sets the width of a column of text. It's useful when you are making tables. This way you can manually set the width of a column so that everything is consistent.

The fixed and setprecision(#) commands relate to each other. The whole statement basically says that any numerical output that follows the statement will have a fixed decimal and displays two places after the decimal (25.32, 33.79, etc.). It is quite handy when you want to format numbers into dollars.

Yes, alot of the coding seems like a waste, but it was the basis for what we would code later on. We redid this program two more times, each time was more complex, but simplified.

Declaring your variables at the beginning of the program isn't a required standard, but it is good programming ettiquete (sp?). Say you are working for a software company and you are working in teams. You and everyone else in your team are sharing the same code. Let's say you had to correct an error in a section of coding your teammate coded. Which is easier, finding the variables at the beginning of the program or finding the variables buried in thousands of lines of code?

Curious, how would you revise my code so that it takes up only 20 or so lines?

As for recommendations on your program...

First, it is a nice and handy little program.

Second, in the code itself, add some spaces between blocks of coding. Use the extra space to break up your if-else and case statements. That way you can find the statements easier.

Third, declare your variables at the beginning of the program and initialize them up there too. If you're declaring an integer, you might as well initialize it to 0 or something else. By doing it this way, you minimize the chance that the program will assign it some random value from memory.

Fourth, add comments.

Fifth, work on some different formats for your output while the code is running. A lot of my code looks like a big old waste because most of it is formatting for my output. I am good for putting most things into some sort of table for output because it is easy to read and understand. You don't really need that kind of output for some simple conversions, but keep it in mind for future programs.

Other than that, good job. Let me know if and when you want to see some of our more advanced programs.
Image

User avatar
Leviathen
Colonel
Posts:1450
Joined:Mon Mar 28, 2005 12:51 am
Location:Ontario, Canada
Contact:

Post by Leviathen » Tue May 23, 2006 10:07 pm

/me snores
Image

Sealed with a curse as sharp as a knife, doomed is your soul and damned is your life

Issele
Captain
Posts:427
Joined:Sat Dec 24, 2005 8:30 pm

Post by Issele » Wed May 24, 2006 5:22 am

Kurke_Aumea wrote:
Apium wrote:I wiped my computer clean yesterday so I don't have my compiler right now. I get the idea though. Its more advanced than my program would have been. I could have done the same thing add about 20 lines of code though.

There are a couple questions I have:
What are the setw(a number)??
What is this line "cout << fixed << setprecision(2);"???

Thats it. I figure while we are on programming I might as well learn something.


The setw(#) command sets the width of a column of text. It's useful when you are making tables. This way you can manually set the width of a column so that everything is consistent.

The fixed and setprecision(#) commands relate to each other. The whole statement basically says that any numerical output that follows the statement will have a fixed decimal and displays two places after the decimal (25.32, 33.79, etc.). It is quite handy when you want to format numbers into dollars.

Yes, alot of the coding seems like a waste, but it was the basis for what we would code later on. We redid this program two more times, each time was more complex, but simplified.

Declaring your variables at the beginning of the program isn't a required standard, but it is good programming ettiquete (sp?). Say you are working for a software company and you are working in teams. You and everyone else in your team are sharing the same code. Let's say you had to correct an error in a section of coding your teammate coded. Which is easier, finding the variables at the beginning of the program or finding the variables buried in thousands of lines of code?

Curious, how would you revise my code so that it takes up only 20 or so lines?

As for recommendations on your program...

First, it is a nice and handy little program.

Second, in the code itself, add some spaces between blocks of coding. Use the extra space to break up your if-else and case statements. That way you can find the statements easier.

Third, declare your variables at the beginning of the program and initialize them up there too. If you're declaring an integer, you might as well initialize it to 0 or something else. By doing it this way, you minimize the chance that the program will assign it some random value from memory.

Fourth, add comments.

Fifth, work on some different formats for your output while the code is running. A lot of my code looks like a big old waste because most of it is formatting for my output. I am good for putting most things into some sort of table for output because it is easy to read and understand. You don't really need that kind of output for some simple conversions, but keep it in mind for future programs.

Other than that, good job. Let me know if and when you want to see some of our more advanced programs.



"And I was like....'Hrm?'"


http://spherule.com/media/video/switch/ellen_feiss.mov

Issele
Captain
Posts:427
Joined:Sat Dec 24, 2005 8:30 pm

Post by Issele » Wed May 24, 2006 5:22 am


Ri'Me

Post by Ri'Me » Wed May 24, 2006 5:55 am

All of this you dont have to know already going in to college, right? I mean..

User avatar
Leviathen
Colonel
Posts:1450
Joined:Mon Mar 28, 2005 12:51 am
Location:Ontario, Canada
Contact:

Post by Leviathen » Wed May 24, 2006 6:09 am

Issele wrote:
Leviathen wrote:/me snores


http://www.albinoblacksheep.com/flash/ayumu


o___o;
Image

Sealed with a curse as sharp as a knife, doomed is your soul and damned is your life

Locked