Problems getting printf to print the products of two variables

This is a discussion on Problems getting printf to print the products of two variables within the C Programming forums, part of the General Programming Boards category; I am new to coding this is the first programming language I have found that was not overly complicated so ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    12

    Post Problems getting printf to print the products of two variables

    I am new to coding this is the first programming language I have found that was not overly complicated so I decided I would try to learn this as a first language. I have been doing ok. I have been slowly messing around with tutorials and stuff on the net for a little while. I have decided to make my first serious program (Up till now I have just been making things like hello world programs and what not) I decided to make a small calculator that would calculate the area of a square and as I learned more I could expand upon it to add more features. What I am having problems with is getting printf to print the product of two variables. It does print a number but it does not print the correct number and no matter what numbers you type in it always comes up with the same answer. I will post my code below. if any one could tell me what I am doing wrong I would be very thankful. Any constuctive criticism is also welcome, I am here to learn after all.

    Code:
    #include <cstdlib>
    #include <iostream>  
    
    using namespace std;
    
    int main( int a, int le, int argc, int ar, char *argv[])
    {
        ar = (le*2);                                                       /*Declares that ar is the same as le X 2*/
        printf("----------------------------------------------\nWelcome to my simple Area Calculation Program!\n----------------------------------------------\n"); /*prints words to the screen*/
        printf("Lets get started shall we?!\n\n");
        system("PAUSE");                                                   /*Pauses untill the user presses a key*/
        system("CLS");                                                     /*Clears the screen*/
        /*Begining of menu*/
        printf("   Select the shape you wish to calculate the area of\n"); 
        printf("-> 1 - Square\n");
        printf("-> 2 - Rectangle (Comming soon)\n");
        printf("-> 3 - Circle (Comming soon)\n");
        printf("-> ");
        scanf("%d", &a);    /*Asks the user for input, then asigns the input to "a"*/
        /*End of menu*/
        if("&a == 1");      /*Declares that if a is equal to 1 then perform the following*/
        printf("Please enter the Length of one side of the square: ");
        scanf("%d",&le);    /*Asks the user for input, then asigns the input to "le"*/
        printf("\n");
        printf("The area of the square is %d",ar);     /*prints the product of le*le*/
        printf("\n\n");
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Calculate the result after you obtain input. And use a standard definition for main (see the FAQ if you don't understand what I meant). And use C headers instead of C++ headers.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    Hey, thanks for the help. I have corrected a few things but it still does not seem to be working properly. I think I got the standard definition for main fixed and I think I got the headers for C worked out. However I can not figure out what you mean by "Calculate the result after you obtain input" from what I can tell the program is doing just that, or am I missing somthing critical here. I will post the revised code below.

    Code:
    #include <cstdlib>
    #include <stdio.h>
    
    using namespace std;
    
    int main( int argc, char *argv[])
    {
    int a,le,ar;
        ar = (le*2);                                                       /*Declares that ar is the same as le X 2*/
        printf("----------------------------------------------\nWelcome to my simple Area Calculation Program!\n----------------------------------------------\n"); /*prints words to the screen*/
        printf("Lets get started shall we?!\n\n");
        system("PAUSE");                                                   /*Pauses untill the user presses a key*/
        system("CLS");                                                     /*Clears the screen*/
        /*Begining of menu*/
        printf("   Select the shape you wish to calculate the area of\n"); 
        printf("-> 1 - Square\n");
        printf("-> 2 - Rectangle (Comming soon)\n");
        printf("-> 3 - Circle (Comming soon)\n");
        printf("-> ");
        scanf("%d", &a);    /*Asks the user for input, then asigns the input to "a"*/
        /*End of menu*/
        if("&a == 1");      /*Declares that if a is equal to 1 then perform the following*/
        printf("Please enter the Length of one side of the square: ");
        scanf("%d",&le);    /*Asks the user for input, then asigns the input to "le"*/
        printf("\n");
        printf("The area of the square is %d",ar);     /*prints the product of le*le*/
        printf("\n\n");
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( int argc, char *argv[])
    {
    int a,le,ar;
        printf("----------------------------------------------\nWelcome to my simple Area Calculation Program!\n----------------------------------------------\n"); /*prints words to the screen*/
        printf("Lets get started shall we?!\n\n");
        system("PAUSE");                                                   /*Pauses untill the user presses a key*/
        system("CLS");                                                     /*Clears the screen*/
        /*Begining of menu*/
        printf("   Select the shape you wish to calculate the area of\n");
        printf("-> 1 - Square\n");
        printf("-> 2 - Rectangle (Comming soon)\n");
        printf("-> 3 - Circle (Comming soon)\n");
        printf("-> ");
        scanf("%d", &a);    /*Asks the user for input, then asigns the input to "a"*/
        /*End of menu*/
        if("&a == 1");      /*Declares that if a is equal to 1 then perform the following*/
        printf("Please enter the Length of one side of the square: ");
        scanf("%d",&le);    /*Asks the user for input, then asigns the input to "le"*/
        printf("\n");
        ar = (le*2); /* calculate the output after getting input */
        printf("The area of the square is %d",ar);     /*prints the product of le*le*/
        printf("\n\n");
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    [edit]Calculating the square would be like this.
    Code:
    ar = (le*le);
    Last edited by Dave_Sinkula; 01-09-2006 at 02:17 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    Oh I see....so I had to asign a value to ar, after the user asigned a value to le. Thanks for all your help. Program seems to be working much better now.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    Ok I have gotten it so that all works properly thanks for your help. I was also wondering if any one could tell me how I could add other options. Currently what my program does is when you enter 1 in the program it lets you calculate the area of a square. What I need to know is how to make it so that when you type in 2 it will let you perform a different task. I have tryed using another if statement but it just uses it as if there where no option and runs it in a straight line. I have also tried using a else if statement but it comes up as gaving an error in it when I try to compile it. Can any one tell me the correct way to use an Else If statement? This is how I have been using it. Can you please try to explain what is wrong with it?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    int a,le,ar,he;
        printf("----------------------------------------------\nWelcome to my simple Area Calculation Program!\n----------------------------------------------\n");
        system("PAUSE");
        system("CLS");
        printf("   Select the shape you wish to calculate the area of\n"); 
        printf("-> 1 - Square\n");
        printf("-> 2 - Rectangle (Comming soon)\n");
        printf("-> 3 - Circle (Comming soon)\n");
        printf("-> ");
        scanf("%d", &a);
    if("&a == 1");
        printf("Please enter the length of one side of the square: ");
        scanf("%d",&le);
        printf("\n");
        ar = (le*le);
        printf("The area of the square is %d",ar);
        printf("\n\n");
        printf("Thank you for using my area calculation program\n\nTo report any errors or to send ideas for future versions\nPlease E-mail me at Jlc_1988@hotmail.com \n");
        system("PAUSE");
    else if("&a == 2")
        printf("Please enter the length of the rectangle");
        scanf("%d",&le);
        printf("Please enter the heigth of the rectangle: ");
        scanf("%d",&he);
        ar = (le*he);
        printf("The area of the square is %d",ar);
        return EXIT_SUCCESS;
    }

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    "&a == 1" is a stringliteral.
    if used as a boolean it allwais evaluates to true.
    Code:
    if("&a == 1");
    You have put a semicolon after the if statment so that evaluates to if true do nothing.
    you want
    Code:
    if(a == 1) { // start a block
        .... // code that should get executed if a eq. 1
    }
    else if(a == 1) {
        .... // code that should get executed if a eq.  2
    }
    BTW the expression
    Code:
    if(&a == 1)
    would mean if the adress of a eq. 1 ....

    Kurt

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    Thanks for the info.....it is working great now. This site is probably the best thing I have found to help me learn :P

  9. #9
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    If you're going to have more choices (say 3, 4, 5, and 6), you might want to consider using a switch statement instead of multiple if-else if statements. It's a bit easier to read. And if your range of values is small, the performance might be better depending on your compiler.

    Here's an example:

    Code:
    switch (a) {
    
       case 1:
             /* do area of square here */
          break;
       case 2:
            /*  do area of rectangle here */
          break;
       case 3:
            /* do something else here */
          break;
        case 4:
           /* do something else here */
          break;
        case 5:
            /* do something different here */
          break;
        default:
            /* for all values that come in that you are not specifically
                handling
             */
           break;
    }

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    Ok I will definatly try to figure that switch thing out. If I dont get it I will post on here. Thanks for the tip.

  11. #11
    old man
    Join Date
    Dec 2005
    Posts
    90
    You also might find it more convenient to format long lines like this:

    Code:
      printf("----------------------------------------------\n"
             "Welcome to my simple Area Calculation Program!\n"
             "----------------------------------------------\n");
    This makes them a lot easier to read in your source.

  12. #12
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    Ok here is an edited version of my code. I would appreciate any input (good or bad).

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        int num1,num2,plus,sub,le,ar,he,main,a,mul;
        printf("-----------------------------------------\n"
               "Welcome to my simple Calculation Program!\n"
               "-----------------------------------------\n"
               "(c) copyright 2005 James L. Curtis\n"
               "-----------------------------------------\n");
        system("PAUSE");
        system("CLS");
        printf("   Main Menu \n"); 
        printf("-> 1 - Addition\n");
        printf("-> 2 - Subtraction\n");
        printf("-> 3 - Multiplication(coming soon)\n");
        printf("-> 4 - Area Calculation\n");
        printf("-> ");
        scanf("%d", &main);
       switch (main) {
    
       case 1:
         printf("Please enter a number ");
         scanf("%d",&num1);
         printf("Please enter another number ");
         scanf("%d",&num2);
         plus = (num1+num2);
         printf("Your answer is %d\n\n",plus);
          break;
       case 2:
         printf("Please enter a number ");
         scanf("%d",&num1);
         printf("Enter the ammount you wish to subtract from the first number ");
         scanf("%d",&num2);
         sub = (num1-num2);
         printf("Your answer is %d\n\n",sub);    
          break;
       case 3:
         printf("Please enter a number ");
         scanf("%d",&num1);
         printf("Enter the ammount you wish to multiply the first by ");
         scanf("%d",&num2);
         mul = (num1*num2);
         printf("Your answer is %d\n\n",mul);    
          break;
        case 4:
         printf("   Select the shape you wish to calculate the area of\n"); 
         printf("-> 1 - Square\n");
         printf("-> 2 - Rectangle\n");
         printf("-> 3 - Circle (Comming soon)\n");
         printf("-> ");
         scanf("%d", &a);
        if(a == 1){
         printf("Please enter the length of one side of the square: ");
         scanf("%d",&le);
         printf("\n");
         ar = (le*le);
         printf("The area of the square is %d",ar);
         printf("\n\n");
    }
    else if(a == 2){
        printf("Please enter the length of the rectangle: ");
        scanf("%d",&le);
        printf("Please enter the heigth of the rectangle: ");
        scanf("%d",&he);
        ar = (le*he);
        printf("The area of the square is %d",ar);
        printf("\n\n");
        }
          break;
    }
      printf("Thank you for using Curtis Calculator v1.2\n\nTo report any errors or to send ideas for future versions\n"
              "Please E-mail me at Jlc_1988@hotmail.com \n\n");
      system("PAUSE");	
      return 0;
    }

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    It is not a good idea to use two similar symbols like this:
    Code:
    int main(int argc, char *argv[])
    {
        int num1,num2,plus,sub,le,ar,he,main,a,mul;
    Consistent indentation is a plus.

    Using scanf can be a chore. Other ways to get a number:
    FAQ > How do I... (Level 1) > How do I get a number from the user (C)
    fgets/sscanf is generally a good choice.

    Functionizing repetitive code is a good habit.

    [edit]You may want to avoid this.
    Code:
    system("PAUSE");
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    And this:
    Code:
    system("CLS");
    Code for a while and such things are annoying.
    Last edited by Dave_Sinkula; 01-11-2006 at 11:37 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    ummm
    well how else would u pause your prgram
    or clear the screen?
    and u mean u change the second one right

    It is not a good idea to use two similar symbols like this:
    Code:
    int main(int argc, char *argv[])
    {
    int num1,num2,plus,sub,le,ar,he,main,a,mul;Consistent indentation is a plus.

  15. #15
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by LoRdHSV1991
    well how else would u pause your prgram.
    I would not do that. Pausing a console-program makes it useless for input redirection.
    or clear the screen?
    A console program should not assume that there is a screen at all. stdout could as well be a printer. ( how to clear that )
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 03:58 PM
  2. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  3. printf wont print without newline
    By rupurt in forum C Programming
    Replies: 4
    Last Post: 10-24-2005, 10:05 AM
  4. function problems
    By money in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2003, 12:40 PM
  5. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 07:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21