d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Computer Programming > Need Help With Simple Program
Prev12345Next
Add Reply New Topic New Poll
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 14 2014 09:42pm
Quote (bensfriend1 @ Feb 14 2014 11:39pm)
#include <iostream>
#include <iomanip>
#include <string>

@abduct what do the first two mean besides just the string? can i remove those as well?

And the C to F code doesnt need error checking as far as i can tell on the project outline

I believe I need the redundant error and more code because the project really wants the proper error messages.


You need the first two because they give you access to the core C++ IO manipulation functions (cin, cout, etc).
Member
Posts: 161,550
Joined: Oct 18 2006
Gold: 4.03
Warn: 20%
Feb 14 2014 09:48pm
Quote (bensfriend1 @ Feb 14 2014 05:46pm)
this looks like it would work, but I have to do it the very specific way my professor wants it and C++ compared to your java are different even though im sure they get the same result.



theres a programming section? please link me up! unless thats just the miscellaneous help for computers now.


that's just simple math and making sure you point the correct question to the correct equation.. math is the same in every language.. there's no fancy way to do math, that's why if aliens landed on our planet you show them the fucking pythagorean theorem and they'll be like oh fuck these monkeys aren't that fucking stupid afterall
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 14 2014 09:50pm
Quote (GRATS @ Feb 14 2014 11:48pm)
that's just simple math and making sure you point the correct question to the correct equation.. math is the same in every language.. there's no fancy way to do math, that's why if aliens landed on our planet you show them the fucking pythagorean theorem and they'll be like oh fuck these monkeys aren't that fucking stupid afterall


Then they show you the math behind quantum mechanics and they be like "wtf these apes are dumb."
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Feb 14 2014 11:05pm
Quote (AbDuCt @ 14 Feb 2014 22:31)
If you use your operators correctly you can eliminate all your redundant error checking.

Code
int main() {
    cout << "Input a type and an amount (K, M, F, C): ";
    cin >> unit >> amount;

    if (unit == 'K' && amount >= 0) {
        cout << "Distance " << amount << " kilometers is "
        << amount * MILES_PER_KM << " miles.";
    }
    else if (unit == 'M' && amount >= 0) {
        cout << "Distance " << amount << " miles is "
        << amount * KM_PER_MILE << " kilometers.";
    }
    else if (unit == 'F' && amount <= MAX_F_TEMP && amount >= MIN_F_TEMP) {
        cout << "Temperature " << amount << " F is "
        << ( amount - FREEZING_F ) * CELSIUS_PER_FAHR << " C.";
    }
    else if ( unit == 'C' ) {
        cout << "Temperature " << amount << " C is "
        << amount * FAHR_PER_CELSIUS + FREEZING_F << " F.";
    }
    else {
        cout << "UNABLE TO PROCESS: " << unit
        << " is not a recognized measurement." << endl;
        cout << "Please enter either K, M, F, or C and a value above 0" << endl;
    }

    return 0;
}


It limits your specific error messages for each if branch but it reduces the amount of code. Only do this if your project does not state you need proper error messages.

You can also remove your <string> include as you are not using any string manipulation functions.

Also double check it compiles and works properly after any changes.

edit:: Also your Celsius to Fahrenheit if branch does not have any error checking. Not sure if this was by design.

edit2:: I am also not a fan of the expanded style of C I much rather condense everything as shown in the edited code. Although again it is up to how you are being taught style wise. Be sure you wont lose marks for using your own coding style.


OMG. Hahahaha :lol: I was just thinking to myself as I read his code. "Hmm. No comments, no && operators, no knowledge of float, int, double, long, etc. Oh man..."

Quote (bensfriend1 @ 14 Feb 2014 22:39)
#include <iostream>
#include <iomanip>
#include <string>

@abduct what do the first two mean besides just the string? can i remove those as well?

And the C to F code doesnt need error checking as far as i can tell on the project outline

I believe I need the redundant error and more code because the project really wants the proper error messages.


Oh man. You should be in a basic course bud. Not sure who taught you but they didn't give you any fundamentals it seems.
Read this:
STARTING OUT WITH C++ From Control Structures through Objects SEVENTH EDITION

http://www.amazon.com/s/?ie=UTF8&keywords=starting+out&tag=googhydr-20&index=stripbooks&hvadid=19931788157&hvpos=1t2&hvexid=&hvnetw=s&hvrand=13867330391977755615&hvpone=&hvptwo=&hvqmt=b&hvdev=c&ref=pd%5Fsl%5F92zzw19zud%5Fb

It goes through all the basics very well. To answer your original questiom: int is short for integer, float for floating precision point, double for double floating precision point, char is character. IIRC it is 8 bits or 1 byte of data. I think it is the smallest unit in C++ but I could be wrong.**

Code
Integer literal 20
String literal "Today we sold "
String literal "bushels of apples.\n"
Integer literal 0


Also:

Code
[B]Data Type Size Range[/B]
short 2 bytes -32,768 to +32,767
unsigned short 2 bytes 0 to +65,535
int 4 bytes -2,147,483,648 to +2,147,483,647
unsigned int 4 bytes 0 to 4,294,967,295
long 4 bytes -2,147,483,648 to +2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
  • Integers are at least as big as short integers.
  • Long integers are at least as big as integers.
  • Unsigned short integers are the same size as short integers.
  • Unsigned integers are the same size as integers.
  • Unsigned long integers are the same size as long integers.
In stead of const you can use #define for keep a variable from being modified.

Code
#define PI 3.14159
#define DIAMETER 10.0


The #include <iostream> is a preprocessor directive. It goes before the compiler and inputs all the data from the directive. It's much like calling a library in Java. It instead of writing the crap load of code it takes to do a cout//cin, etc. (By the way cout stands for console back when people typed on terminals to access consoles.) Same with <stings>, etc.; they are called header files. Hence they go at the head of the file.
iostream, string, etc. is also the name of the file being called forth into the source code.

Well I am tired and that is enough of that today.

Edit:: Left a sentence half written.**

This post was edited by NinjaSushi2 on Feb 14 2014 11:08pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 15 2014 12:24am
Quote (NinjaSushi2 @ Feb 15 2014 12:05am)
OMG. Hahahaha :lol: I was just thinking to myself as I read his code. "Hmm. No comments, no && operators, no knowledge of float, int, double, long, etc. Oh man..."


He's learning.


Quote

Oh man. You should be in a basic course bud. Not sure who taught you but they didn't give you any fundamentals it seems.


He is in a basic course.

Quote
In stead of const you can use #define for keep a variable from being modified.


This one made my head hurt. Don't do this. The only benefit to doing this over declaring constants is you save a bit of memory not having to allocate memory for each variable stored on the names table. The preprocessor just replaces the define with the literal. You sacrifice scope capabilities though. If you don't need global scope for a variable, you should not be using #define. In fact, saying you should replace const with #define as a rule of thumb is just a bad idea.


Quote

The #include <iostream> is a preprocessor directive. It goes before the compiler and inputs all the data from the directive. It's much like calling a library in Java. It instead of writing the crap load of code it takes to do a cout//cin, etc.

It is nothing like calling a library in Java. The preprocessor literally just replaces the #include with the contents of the file. This happens before compilation, so the compiler just sees 1 source file after the preprocessor does its parsing. The java import statement just tells the compiler where to look for external classes. They are 2 different operations.

Quote

(By the way cout stands for console back when people typed on terminals to access consoles.)

cout represents the standard output stream. It stands for C OUTput stream. And it doesn't just print to a console. It prints to the designated default output stream, which by default is the console. But it can be set to log, err, a file, etc.

Quote
Same with <stings>, etc.; they are called header files. Hence they go at the head of the file.
iostream, string, etc. is also the name of the file being called forth into the source code.


They aren't called headers because they go in the beginning (hint: they dont have to go at the top of the file). They are called headers because they precede the actual implementation. Headers are things typically "describe" how to read/operate something. In the case for C++ they typically contain function declarations. The actual implementations of these functions will be in the main cpp file, which will then #include the header to grab the declarations.It allows for multiple implementations of the same interface to exist. Again, all the preprocessor does is take the contents of one file and paste it where the #include was. There is no rule that says this must go at the "head" of the file.


Don't bag on the kid because he is learning, especially when you have a lot to learn as well..

Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 15 2014 12:42am
+1 Minkomonster
Member
Posts: 23,838
Joined: Feb 18 2009
Gold: 0.01
Feb 15 2014 12:51am
Quote (Minkomonster @ Feb 14 2014 10:24pm)
He's learning.




He is in a basic course.



This one made my head hurt. Don't do this. The only benefit to doing this over declaring constants is you save a bit of memory not having to allocate memory for each variable stored on the names table. The preprocessor just replaces the define with the literal. You sacrifice scope capabilities though. If you don't need global scope for a variable, you should not be using #define. In fact, saying you should replace const with #define as a rule of thumb is just a bad idea.



It is nothing like calling a library in Java. The preprocessor literally just replaces the #include with the contents of the file. This happens before compilation, so the compiler just sees 1 source file after the preprocessor does its parsing. The java import statement just tells the compiler where to look for external classes. They are 2 different operations.


cout represents the standard output stream. It stands for C OUTput stream. And it doesn't just print to a console. It prints to the designated default output stream, which by default is the console. But it can be set to log, err, a file, etc.



They aren't called headers because they go in the beginning (hint: they dont have to go at the top of the file). They are called headers because they precede the actual implementation. Headers are things typically "describe" how to read/operate something. In the case for C++ they typically contain function declarations. The actual implementations of these functions will be in the main cpp file, which will then #include the header to grab the declarations.It allows for multiple implementations of the same interface to exist. Again, all the preprocessor does is take the contents of one file and paste it where the #include was. There is no rule that says this must go at the "head" of the file.


Don't bag on the kid because he is learning, especially when you have a lot to learn as well..


:hail:
Member
Posts: 161,550
Joined: Oct 18 2006
Gold: 4.03
Warn: 20%
Feb 15 2014 02:06am
Quote (AbDuCt @ Feb 14 2014 08:50pm)
Then they show you the math behind quantum mechanics and they be like "wtf these apes are dumb."


It's simple, our space ship turns into light and goes light speed * 500

thus it is fast

mad?
Member
Posts: 105,139
Joined: Apr 25 2006
Gold: 10,475.00
Feb 15 2014 02:12am
Quote (GRATS @ Feb 15 2014 03:06am)
It's simple, our space ship turns into light and goes light speed * 500

thus it is fast

mad?



Hey now. Who you callin' Tachyons ?
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Feb 15 2014 05:45am
Quote (Minkomonster @ 15 Feb 2014 01:24)
He's learning.




He is in a basic course.



This one made my head hurt. Don't do this. The only benefit to doing this over declaring constants is you save a bit of memory not having to allocate memory for each variable stored on the names table. The preprocessor just replaces the define with the literal. You sacrifice scope capabilities though. If you don't need global scope for a variable, you should not be using #define. In fact, saying you should replace const with #define as a rule of thumb is just a bad idea.



It is nothing like calling a library in Java. The preprocessor literally just replaces the #include with the contents of the file. This happens before compilation, so the compiler just sees 1 source file after the preprocessor does its parsing. The java import statement just tells the compiler where to look for external classes. They are 2 different operations.


cout represents the standard output stream. It stands for C OUTput stream. And it doesn't just print to a console. It prints to the designated default output stream, which by default is the console. But it can be set to log, err, a file, etc.



They aren't called headers because they go in the beginning (hint: they dont have to go at the top of the file). They are called headers because they precede the actual implementation. Headers are things typically "describe" how to read/operate something. In the case for C++ they typically contain function declarations. The actual implementations of these functions will be in the main cpp file, which will then #include the header to grab the declarations.It allows for multiple implementations of the same interface to exist. Again, all the preprocessor does is take the contents of one file and paste it where the #include was. There is no rule that says this must go at the "head" of the file.


Don't bag on the kid because he is learning, especially when you have a lot to learn as well..


Hmm interesting read.

I never said use the #define as a replacement for const, I said you can use it in placement of const. He should know that as some older programs written still use #define.

The java reference was a poor comparison, you are correct.

Regarding cout:

Quote
The simplest type of screen output that a program can display is console output, which is merely plain text. The word console is an old computer term. It comes from the days when a computer operator interacted with the system by typing on a terminal. The terminal, which consisted of a simple  screen and keyboard, was known as the console. On modern computers, running graphical operating systems such as Windows or Mac OS X, console output is usually displayed in a window. In C++ you use the cout object to produce console output. (You can think of the word cout as meaning console output.)


That is how I learned cout at least. Console OUTput as a learning tool because it makes more sense that way.

Obviously there is no rule that says it has to go at the top of the file. Nor did I said you couldn't place it anywhere else as a preprocessor super-ceeds any other code written. It's written at the top of the file because that is good programing. Same with putting comment lines through your program and having your program open with a comment. Such as the name of the programer who wrote it, last date modified, what the file/program does, etc.

You are right though. It was wrong to laugh, my bad. :(

Thanks for correcting me on anything. :)

This post was edited by NinjaSushi2 on Feb 15 2014 06:02am
Go Back To Programming & Development Topic List
Prev12345Next
Add Reply New Topic New Poll