![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |||
| Please tell me where I messed up In the Code below i have written for my class i am unsure what I have done wrong. This is suppose to be a calculator for different shapes. This is the instructions from my professor "If the user enters a character other than t, c, s, or r, tell him that he has to enter one of those four characters, and then have your program exit. On the other hand, if the user enters a t, you then need to prompt him to enter the base and the height of the triangle. If he enters a c, you need to prompt him to enter the radius of the circle. If he enters an s, you need to prompt him to enter the length of the side of the square. And if he enters an r, you need to prompt him to enter the length and the width of the rectangle." #include <stdio.h> #include <math.h> void main () { double num, num2, total; char shape; char enterkey; printf ("Please enter a shape:"); scanf ("%c", &shape); if (shape=='t'){ printf ("\nPlease enter the base of the triangle:"); scanf ("%d", &num); printf ("\nPlease enter the height of the triangle:"); scanf ("%d", &num2); total= 0.5 * num * num2; printf ("area= 0.5 * %d * %d = %d", num, num2, total); }else if (shape=='c'){ printf ("\nPlease enter the radius of the circle:"); scanf ("%d", &num); total=2 * 3.14 * num; printf ("area= 2 * 3.14 * %d = %d", num, total); }else if (shape=='s'){ printf ("\nPlease enter the side of the square:"); scanf ("%d", &num); total= 4 * num; printf ("area= 4 * %d = %d", num, total); }else if (shape=='r'){ printf ("\nPlease enter the length of the rectangle:"); scanf ("%d", &num); printf ("\nPlease enter the height of the rectangle:"); scanf ("%d", &num2); total = num * num2; printf ("area= %d * %d = %d", num, num2, total); }else { printf ("\n I do not know that letter that was inputed please try again!"); } printf ("\n\n"); scanf ("%c", &enterkey); } Last edited by hnkplaya : 06-28-2007 at 10:15 AM. |
| |
| |||
| Quote:
It tells me "error C2047: illegal default" |
| ||||
| I'm not sure with formatted print using C languages, what do you guys use for double, float, and integers? I just wanted to make sure that your code doesn't have any errors from that. Is there regular println in C languages? What is it? Use it, I think it's much simpler for me than printf.... hnkplaya, can you please post your error for your first code? |
| |||
| int printf (char* format, ...); where format is one of the following: "%hd" or "%hi" - signed short int "%hu" - unsigned short int "%d" or "%i" - signed int "%u" - unsigned int "%ld" or "%li" - signed long int "%lu" - unsigned long int "%f" - float "%lf" - double "%Lf" - long double "%hx" - unsigned hexadecimal short int (lowercase letters) "%x" - unsigned hexadecimal int (lowercase) "%lx" - unsigned hexadecimal long int (lowercase) "%hX" - unsigned hexadecimal short int (uppercase letters) "%X" - unsigned hexadecimal int (uppercase) "%lX" - unsigned hexadecimal long int (uppercase) "%ho" - signed octal short int "%o" - signed octal int "%lo" - signed octal long int "%c" - single character "%s" - string of characters (character array, a.k.a. "C(-style) string" or "ASCIIZ string") "%p" - pointer address "%n" - nothing printed, but the argument should be a signed int where the number of characters written so far should be stored "%%" - output a percent (%) sign (since the percent sign is reserved for denoting the position where arguments should be inserted) I hope that helps. Link for ALL of the specifics of the format string for the printf/scanf family of functions: printf - C++ Reference Edit: Regarding the "illegal default" error, make sure your default case is inside the switch statement and that you closed the switch statement (sometimes you might close the switch statement accidentally or leave it open when it shouldn't be). Last edited by rpgfan3233 : 07-07-2007 at 03:55 PM. |
| The Following 2 Users Say Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (07-07-2007), TeraTask (07-07-2007) | ||
| ||||
| is printf new in C++ as it is new in Java? Is there println in C++ as it is in Java I've read about cout << "Hello" << something like that, but I'm not sure if you can do that as if you do in println() command ![]() that's because I think using println() is much easier than having to remember all % for printf statement, well it's probably better if you can remember all of them, but I think I'd just print what rpgfan said out and put it in front of your computer or... yeah, go to C++ reference site |
| |||
| Quote:
As for Java's System.out.println (or System.Console.WriteLine in C#), there is no such thing in C++. The closest thing to it is the std::endl member: std::cout << "Hello world!" << std::endl; // C++ // System.out.println("Hello world!"); // Java // System.Console.WriteLine("Hello world!"); // C# Of course, if you included "using namespace std;" at the beginning of the C++ program, then you don't need to type "std::" to show that the member belongs to the std namespace. |
| ||||
| Quote:
Code: case 1:
// do something
break;
case 2:
// do something
break; since programmers write codes so that other programmers can read it!!! XD |
| |||
| If you use functions for each case, the only advantage would be in a large switch statement. As long as code is indented enough, there shouldn't be much problem. Of course, if the issue is redundant code, that usually means that you can structure your code better. For example: Code: printf("The area of the ");
if (shape == 'r')
printf("rectangle");
else if (shape == 'c')
printf("circle");
// etc.
printf(" is %d\n", area); Code: char shape_name[10]; //"rectangle" is 9 characters, plus the null terminator makes 10
switch (shape) {
case 'r':
strcpy(shape_name, "rectangle");
// rest of code for rectangle here
break;
case 'c':
strcpy(shape_name, "circle");
// rest of code for circle here
break;
//other cases and default
}
total = num1 * num2;
printf("The area of the %s is %lf\n", shape_name, total); __________________ "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." -- Bjarne Stroustrup, creator of what is now known as C++ For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that. |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (07-27-2007) | ||
![]() |
| Thread Tools | |
| Display Modes | |
| |