The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


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.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 06-28-2007, 09:33 AM
hnkplaya hnkplaya is offline
Novice
Join Date: Jun 2007
Posts: 7
iTrader: (0)
hnkplaya is on a distinguished road
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);
}

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by hnkplaya : 06-28-2007 at 10:15 AM.
Reply With Quote
  #2 (permalink)  
Old 06-28-2007, 10:17 AM
siLenTz's Avatar
siLenTz siLenTz is offline
Jr. Programmer
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Posts: 87
iTrader: (0)
siLenTz will become famous soon enoughsiLenTz will become famous soon enough
I am C++ programmer, and I think your code is in C right? It is little bit
messy. I think you should use switch() instead of using too many
elseif. Try this

Code:
switch(shape) {
   case 't':

      printf ("\nPlease enter the base of the triangle:");
        scanf ("%f", &num);
       printf ("\nPlease enter the height of the triangle:");
       scanf ("%f", &num2);
       total= 0.5 * num * num2;
       printf ("area= 0.5 * %d * %d = %d", num, num2, total);
      break;

    case 'c':

     // your code....
     break;

    default:
         printf ("\n I do not know that letter that was inputed please try again!");
}
Anyway, is it syntax error or logical error? If it is syntax error pleae
post your errors in here. if it is logical error then tell what happen.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #3 (permalink)  
Old 06-28-2007, 10:32 AM
hnkplaya hnkplaya is offline
Novice
Join Date: Jun 2007
Posts: 7
iTrader: (0)
hnkplaya is on a distinguished road
Quote:
Originally Posted by siLenTz View Post
I am C++ programmer, and I think your code is in C right? It is little bit
messy. I think you should use switch() instead of using too many
elseif. Try this

Code:
switch(shape) {
   case 't':
 
      printf ("\nPlease enter the base of the triangle:");
        scanf ("%f", &num);
       printf ("\nPlease enter the height of the triangle:");
       scanf ("%f", &num2);
       total= 0.5 * num * num2;
       printf ("area= 0.5 * %d * %d = %d", num, num2, total);
      break;
 
    case 'c':
 
     // your code....
     break;
 
    default:
         printf ("\n I do not know that letter that was inputed please try again!");
}
Anyway, is it syntax error or logical error? If it is syntax error pleae
post your errors in here. if it is logical error then tell what happen.

It tells me "error C2047: illegal default"

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 06-28-2007, 12:08 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
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?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 07-07-2007, 03:49 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
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).

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by rpgfan3233 : 07-07-2007 at 03:55 PM.
Reply With Quote
The Following 2 Users Say Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-07-2007), TeraTask (07-07-2007)
  #6 (permalink)  
Old 07-07-2007, 05:03 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
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

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #7 (permalink)  
Old 07-07-2007, 05:14 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Quote:
Originally Posted by HelloWorld View Post
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
printf is not new in C++ because C++ evolved from C. You can think of C++ as an extension of C sort of. In C, you include <stdio.h> to use printf. In C++, you include <cstdio>, which refers to the fact that stdio is a C header. Others in C++ include <cstdlib>, <ctime>, etc. (remove the leading 'c' and add ".h" at the end to convert the header notation to C)

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.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #8 (permalink)  
Old 07-27-2007, 08:18 AM
andyp guru
Posts: n/a
Quote:
Originally Posted by hnkplaya View Post
It tells me "error C2047: illegal default"
what about putting the default on the same line?. i think you should make functions for each case that way it makes the source code a lot easier to read.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #9 (permalink)  
Old 07-27-2007, 10:00 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote:
Originally Posted by andyp guru View Post
what about putting the default on the same line?. i think you should make functions for each case that way it makes the source code a lot easier to read.
For some reason, I don't recommend this... I'd rather have it organized this way:

Code:
case 1:
    // do something
    break;
case 2:
    // do something
    break;
it's much easier to understand and very organized. I don't know if it's only for me, hopefully other programmer feel the same way...? since programmers write codes so that other programmers can read it!!! XD

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #10 (permalink)  
Old 07-27-2007, 01:27 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
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);
While it is effective, it isn't exactly ideal. Why not have a variable that holds the name of the shape? Inside the switch statement when figuring out the area of the shape, you could set a string to the value of "rectangle" or "square" or whatever, though the easy way of doing this requires <string.h> (<cstring> in C++, though std::string is better in C++) to be #included:
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);
Just an example of how it might be used. It simplifies the output and doesn't require very much extra code either. That way, there is no need to create your own function and no need to complicate the output.

__________________
"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.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-27-2007)
Reply

« hello | Why C++ »

Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 01:11 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50