newb to C, cant figure something out.

This is a discussion on newb to C, cant figure something out. within the C Programming forums, part of the General Programming Boards category; i JUST started C a few days ago. i decided, after reading a prog in a book i have, to ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    9

    newb to C, cant figure something out.

    i JUST started C a few days ago.

    i decided, after reading a prog in a book i have, to make a program to either multiply or divide two ints depending on user input.

    so i wrote the program, got like 14 compile errors, fixed most of em

    now im down to 2-4 compile errors, and i just cant figure them out.

    either they r too cryptic, or i just don understand yet.

    here are the errors, if you need program source i will post:

    c:\docume~1\galgar~1\desktop\untitl~1.cpp: In function `int main()':
    c:\docume~1\galgar~1\desktop\untitl~1.cpp:17: parse error before `else'
    c:\docume~1\galgar~1\desktop\untitl~1.cpp: At top level:
    c:\docume~1\galgar~1\desktop\untitl~1.cpp:22: parse error before `return'



    actually, here is source:
    Code:
    #include <stdio.h>
    
    int num1, num2, num3, num4;
    int product(int x, int y);
    int divide(int z, int a);
    int main( void )
    {
      printf ("pick Multiply, divide(1,2):");
      scanf ("%d", &num4);
      printf ("enter a number:");
      scanf ("%d", &num1);
      printf ("Enter another number:");
      scanf ("%d", &num2);
      if (num4 == 1)
      num3=product(num1,num2);
      printf ("total multiplied is: %d",num3);
      else
      {
      num3=divide(num1,num2);
      printf ("total divided is: %d",num3);
      }
      return 0;
    }
    int product(int x,int y)
    {
    return (x*y);
    }
    int divide(int z, int a)
    {
    return (z/a);
    }
    please tell me what im doing wrong... i cant figure it out.


    Thanks in Advance,
    Phate

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    should be this
    Code:
      if (num4 == 1)
      {
      num3=product(num1,num2);
      printf ("total multiplied is: %d",num3);
      }
      else
      {
      num3=divide(num1,num2);
      printf ("total divided is: %d",num3);
      }
    "else statement without a matching if"

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    thnx a TON.

    in my book, there was a code for just multiplying, but no if/else statements, so i looked farther in the book

    for some reason it didnt have the brackets... whatever

    thanks a ton for your help!,
    Phate

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    program worked without a hitch! YEAH!!!

    btw, is there a way to make it so if i doubleclick the icon on desktop, it doesnt just autoclose after user input is done?

    as it is, i get this:

    choose multiply or divide (1,2): 1
    Enter a number: 10
    Enter another Number: 2
    (window closes faster then i can read next output)
    The Numbers divided are: 5

    is there a way for it to remain open until i choose to close it?

    also, is there a way that i can make it start over? like after it did the calculation it would go back to choosing multiply or divide?

    i know in C++ u can do a GO thing or whatever, but i dont know it in C

    what is it, and where would i put it?

    thanks a lot,
    Phate

  5. #5
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    btw, is there a way to make it so if i doubleclick the icon on desktop, it doesnt just autoclose after user input is done?
    Throw in a getchar() at the very end of the program

    also, is there a way that i can make it start over? like after it did the calculation it would go back to choosing multiply or divide?
    Stick a while loop over the mult/divide code.

    i know in C++ u can do a GO thing or whatever, but i dont know it in C
    goto is frowned upon, the method is the same for c++, just a big while loop

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    what kind of while loop... like just type while above the first printf?

    sorry, im REALLY noob. need things spelled out for me lol.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    ok, i tried adding another int, Counter

    inside int Main(void) i put the statements: Counter == 1, While (Counter < 1000) and put the program except return 0 in brackets, and put Counter ++ at the end

    now im gettin compile errors again

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    here is the code:
    Code:
    #include <stdio.h>
    
    int num1, num2, num3, num4;
    int Counter;
    int product(int x, int y);
    int divide(int z, int a);
    int main( void )
    {
      Counter == 1
      while (Counter < 1000);
      {
      printf ("pick Multiply, divide(1,2):");
      scanf ("%d", &num4);
      printf ("enter a number:");
      scanf ("%d", &num1);
      printf ("Enter another number:");
      scanf ("%d", &num2);
      if (num4 == 1)
      {
      num3=product(num1,num2);
      printf ("total multiplied is: %d",num3);
      }
      else
      {
      num3=divide(num1,num2);
      printf ("total divided is: %d",num3);
      }
      Counter ++;
      }
      return 0;
    }
    int product(int x,int y)
    {
    return (x*y);
    }
    int divide(int z, int a)
    {
    return (z/a);
    }
    and the compile errors:
    Code:
    c:\docume~1\galgar~1\desktop\untitl~1.cpp: In function `int main()':
    c:\docume~1\galgar~1\desktop\untitl~1.cpp:10: parse error before `while'
    c:\docume~1\galgar~1\desktop\untitl~1.cpp:29: parse error before `}'
    Last edited by Phate4219; 03-03-2006 at 08:50 AM.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    whoops. forgot ; after while and counters... that brings errors down to:
    Code:
    c:\docume~1\galgar~1\desktop\untitl~1.cpp: In function `int main()':
    c:\docume~1\galgar~1\desktop\untitl~1.cpp:10: parse error before `while'
    just assume in previous code that Counter ++ is now Counter ++; and While (Counter < 1000) is now While (Counter < 1000);

    so what is wrong?

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    AHAHAHAHHAHA

    just forgot ;s

    its fixed now lol.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    hmmmmm...

    now when i compile i get NO errors... but when i RUN it, there is no text.

    its just a blank screen.....

    any ideas?

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Just like you don't need a ; after your if statements you don't need a semicolon after your while statement. If you do put one it will simply repeat over and over again until Counter is less than 1000, which will never happen because there is nothing being executed in the loop. So, you need to remove that semicolon.
    Also, you need to set Counter = 1; not ==. == is used for comparing two things, its easier to remember it as "is equals" because it it two equal marks.

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    try this for program termination

    this was my solution to program termination: i wrote a fucntion using the <time.h> header that waits for the specified number of seconds before it terminates, so that output can be seen before termination. If you need the code, I can get it to you.

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    this is the header file for this function:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    /* function uses time.h to determine how long to wait */
      
    void pause(int duration)
    {
     int a, b;
    
     b = clock() / CLOCKS_PER_SEC;
     a = b + duration;
    
     while (b < a)
     {
      b = clock() / CLOCKS_PER_SEC; /* the function will wait until the # of secs is up */
     }
    }
    and here is a sample program I wrote. It should be ok for demostration purposes. Any questions, feel free to ask:

    Code:
    #include <misc.h> 
    
    main ()
    {
     char cmd[5], exitcmd[5] = "exit";
    main:
     for(;;)
     {
      printf(" Hello.\n ");
      pause(3);
      printf(" Would you like to exit? Type exit now\n");
      gets(cmd);
      if ((strcmp(cmd, exitcmd)) == 0)
      {
       return 0;
      }
      goto main;
     }
    }

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > Any questions, feel free to ask:
    Ok, why are you using goto, and why are you using gets() ?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 05:22 PM
  2. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-23-2008, 12:37 AM
  3. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 11:57 AM
  4. Newb C++ Programmer
    By Philandrew in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 09:44 PM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM

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