C++ revision help! - Page 3
Blogs > konadora |
tec27
United States3690 Posts
| ||
konadora
Singapore66063 Posts
#include <iostream> using namespace std; int main() { double c,f; cout << "Please choose whether you are entering the temperature in Centigrade (c) or Farenheit (f)." << endl; if(myChar == 'c'){ f = ((c*1.8)+32); cout << "The temperature is " << f << " degrees Farenheit." << endl; } else if(myChar == 'f'){ c = ((f-32)*5/9); cout << "The temperature is " << c << " degrees Centigrade." << endl; } return 0; } With these errors: 1>c:\users\user\documents\visual studio 2008\projects\p1\p1\p1.cpp(8) : error C2065: 'myChar' : undeclared identifier 1>c:\users\user\documents\visual studio 2008\projects\p1\p1\p1.cpp(12) : error C2065: 'myChar' : undeclared identifier | ||
fusionsdf
Canada15390 Posts
yes at least one of the two numbers must be a float or double, or you must explicitly cast one to a double <static_cast> or integer division will be performed instead. | ||
Superbia
Netherlands8889 Posts
On June 29 2009 01:29 konadora wrote: I don't quite get how to use it, I got this - With these errors: The code in this case would be #include <iostream> using namespace std; int main() { double value; char myChar; cout << "Please enter C (Centigrade) or F (Farenheit) followed by the appropriate value" << endl; cin >> myChar >> value; if(myChar == 'C') cout << "The temperature is " << (value*1.8)+32 << " degrees Farenheit." << endl; else if(myChar == 'F') cout << "The temperature is " << (value-32)*5/9 << " degrees Centigrade." << endl; else cout << "Please insert a correct prefix" << endl; return 0; } Please note how I replaced double f/c with double value and how I moved the calculations to inside the cout << instruction. The example input for this piece of code would be "F 1.42". (note: this would be a rather unpleasant temperature) | ||
konadora
Singapore66063 Posts
Also, the purpose of putting the calculations inside the cout << instruction would be to shorten the entire program, as well as to remove unnecessary variables, right? Also, if you specify 'C' but the user types in 'c' instead (small C), then does it matter? I recall my teacher saying that caps matter a lot in programming. | ||
Superbia
Netherlands8889 Posts
On June 29 2009 01:41 konadora wrote: The char is the same as 'int', except it is not just integers, but characters, that's all, right? char is 8 bit, int is 32 bit (though for int it's compiler specific). I think this might go a bit too far for now, you should use char to save single-byte characters (e.g. 'e', '0', etc). On June 29 2009 01:41 konadora wrote:Also, the purpose of putting the calculations inside the cout << instruction would be to shorten the entire program, as well as to remove unnecessary variables, right? Yeah, it's basically to remove unnecessary variables. | ||
GHOSTCLAW
United States17042 Posts
| ||
konadora
Singapore66063 Posts
| ||
okayokes
United States39 Posts
On June 29 2009 01:41 konadora wrote: The char is the same as 'int', except it is not just integers, but characters, that's all, right? Also, the purpose of putting the calculations inside the cout << instruction would be to shorten the entire program, as well as to remove unnecessary variables, right? Also, if you specify 'C' but the user types in 'c' instead (small C), then does it matter? I recall my teacher saying that caps matter a lot in programming. char holds one ASCII character iirc. If you want a sentence you should use a string. Though you could also use a character array. char myChar[] = {'H', 'e', 'l', 'l', 'o'}; string myString = "Hello"; | ||
Superbia
Netherlands8889 Posts
On June 29 2009 01:35 fusionsdf wrote: yes at least one of the two numbers must be a float or double, or you must explicitly cast one to a double <static_cast> or integer division will be performed instead. With the code "(value-32)*5/9" (value being double) the compiler (MSVS08) generated FPU instructions for 5/9, so in this case it was not necessary. However I see your concern because compilers like to precalculate things. | ||
Divinek
Canada4045 Posts
| ||
konadora
Singapore66063 Posts
Not to mention the one month summer break (which ended today) made me just slack all the way. | ||
Scorch
Austria3371 Posts
You won't make it far without knowing basic concepts like control structures, data types, return values etc. So you should try and learn those basics and then come back if any questions remain. Also, it's far easier for a beginner to modify and extend existing code than to write it yourself. | ||
unknown.sam
Philippines2701 Posts
On June 29 2009 02:35 Scorch wrote: Looks like you are trying to teach yourself programming from scratch without any guidance. That's hard. You should look for a C++ tutorial which explains the basics in an understandable and structured manner. Posing random questions on a very specific problem without quite understanding the answers won't help you much. Especially since those who give advice often look at the problem from a higher level that you don't even need yet. For example, I've seen people talk about compiler settings here, which isn't what you want to know at all and will only confuse you. You won't make it far without knowing basic concepts like control structures, data types, return values etc. So you should try and learn those basics and then come back if any questions remain. Also, it's far easier for a beginner to modify and extend existing code than to write it yourself. this | ||
AoN.DimSum
United States2983 Posts
| ||
okayokes
United States39 Posts
code like this can be hard to get a grip on (atleased for me): void permute(string current, string rest) { if (rest == "") { cout << current << endl; ++counter; }else{ for ( int i = 0; i < rest.length(); i++ ) { string next = current + rest[i]; string remaining = rest.substr(0, i) + rest.substr(i+1); permute(next, remaining); } } } the syntax itself is not the difficult aspect, but keeping track of every stack frame and how it unravels is difficult ~_~. There are some great stanford lectures posted up on youtube though! http://www.youtube.com/profile?user=stanforduniversity&view=playlists Programming Methodology is the beginners course using mostly Java. Programming Abstractions is the intermediate course using C++ Programming Paradigms is the advanced course which I haven't looked at. | ||
DrLefTy
United States36 Posts
| ||
fusionsdf
Canada15390 Posts
On June 29 2009 01:41 konadora wrote: Also, if you specify 'C' but the user types in 'c' instead (small C), then does it matter? I recall my teacher saying that caps matter a lot in programming. since nobody answered this: yes, case matter Variable is different from VARIABLE and vArIaBlE. Generally constants are in all CAPS, normal variables are lowercase with either underscores between words like_this_variable or camel case likeThisVariable. Struct and Class names tend to have the first letter capitalized. Its really important to stay consistent throughout your code. | ||
eci
Germany45 Posts
I think void main() is also not in the recent standard any more, so if you do not know for sure just use "int main()" and don't forget to return a value ( return 0; - you could also write return -1; if an error happend - but its kind of old school ) Btw: This question is rather unimportant - try to write more code - implement some algorithms ( e.g. bread death search in a graph ) - use stl standard library - use external librarys like boost hf in coding | ||
liquorice
United States170 Posts
On June 29 2009 05:53 fusionsdf wrote: since nobody answered this: yes, case matter Variable is different from VARIABLE and vArIaBlE. Generally constants are in all CAPS, normal variables are lowercase with either underscores between words like_this_variable or camel case likeThisVariable. Struct and Class names tend to have the first letter capitalized. Its really important to stay consistent throughout your code. camel notation ftw. there's also another one that has the type in the variable name, but it's fallen out of favour. | ||
| ||