Thread: Hello World! I need help! (not with hello world)

  1. #1
    Registered User K.Aki's Avatar
    Join Date
    Oct 2014
    Posts
    4

    Hello World! I need help! (not with hello world)

    Hi, I am new to the site and new to C programming. In fact I am entirely new to programming, aside from a few encounters I've had in the past with 'hello world' - each time has left me sideling away with my tail between my legs.

    I am pleased to say that after beginning a university degree and requiring to learn C, I can now understand the basic 'codey-bits' to it... just.

    Now the other week we were given a basic running total adding program and then told to code a basic calculator which could take a first number, and +,-,/ or * them to a second number.

    I have the beginnings of the code but after it failed to build and I asked help from my tutor - he deleted my entire code, wrote an entirely different one that had a different function and then spent an hour trying to work out why his code wouldn't work - luckily we found the problem in the end.

    But (if you've managed to stay with me this far) I am still left with my original not-working code... Can anyone see what it is that I am doing and where I have gone wrong? Thank-you so much in advance~

    Here is my code:
    Code:
    #include <stdio.h> 
    #pragma warning(disable: 4996) 
    int main( )
    {
     // Allocate memory (define float number)
     float firstNumber=0.0;
     float secondNumber=0.0;
     char operation;
     // look for a first number from user input
     scanf("%f", &firstNumber);
     // show user their typed first number
     printf("%f", firstNumber);
     // look for an operation (+,-,*,/) from user input
     scanf("%c", &operation);
     // show user their typed first number and opertion
     printf("%f %c", firstNumber, operation);
     // look for a second number from user input
     scanf("%f", secondNumber);
     // show user their typed first number, opertion and last number
     printf("%f %c %f =", firstNumber, operation, secondNumber);
     // upon user pressing enter, calculate (first number) (operation) (second number) and show user the calculated answer
     while()
     {
      if(operation==+)
      {
      printf ("%f", firstNumber + secondNumber);
      }
      if(operation==-)
      {
      printf ("%f", firstNumber - secondNumber);
      }
      if(operation==*)
      {
      printf ("%f", firstNumber * secondNumber);
      }
      if(operation==/)
      {
      printf ("%f", firstNumber / secondNumber);
      }
      
     } // end while
    } // end main
    *I have been told to just include the '#pragma warning(disable: 4996)' because of the software we are using
    *I am starting to think I may not need to include the bit telling the programme to show the user the stuff they have just inputted as I think it will automatically do that, I'm not sure :/

    Again, thank-you if you can help me~ Please treat me as an idiot!

    :3

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    1. Your while loop doesn't have a condition...

    2. In your while loop statement you have if statements that compare characters. You need to put single quotes around the character being compared. like so
    Code:
    if(operation == '+')

  3. #3
    Registered User K.Aki's Avatar
    Join Date
    Oct 2014
    Posts
    4
    Quote Originally Posted by camel-man View Post
    1. Your while loop doesn't have a condition...

    2. In your while loop statement you have if statements that compare characters. You need to put single quotes around the character being compared. like so
    Code:
    if(operation == '+')
    2. Thanks my earlier programme had these but I forgot them this time!!

    1. How do conditions work? What would be a suitable condition? How would I implement it (what would be the syntax)?

    Thank-you so much!!

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Check out the tutorials they have on while loops in C For, While and Do While Loops in C - Cprogramming.com

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    How do conditions work? What would be a suitable condition? How would I implement it (what would be the syntax)?
    Basically, the condition should be something that evalutes to either logical true (non-zero) or logical false (zero).

    Code:
    while (x > 15)    // this will cause the loop to continue executing while x > 15 is true
        // ...
    
    while (y <= 10)   // this will cause the loop to continue executing while y <= 10 is true
        // ...
    
    while (z != 'x')  // this will cause the loop to continue executing while z is not equal
        // ...        // to the value of character 'x'
    
    while (1)         // 1 is always true, so this is an "infinite loop"
        // ...
    The conditions for you loop are determined by how you choose to implement your code. It's up to you to figure out how you want to loop, and under which conditions it should stop looping.

    Also, be sure to include all code you want looped within the body of the loop. In your posted example, the only code that is looped is the printed result. User input is not part of your loop, so that will only be entered once.

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    74
    im also new to c but i think there is also a problem with this line
    Code:
    // look for a second number from user input scanf("%f", secondNumber);


    I think it should read
    Code:
    scanf( "%f" , & secondNumber );

  7. #7
    Registered User K.Aki's Avatar
    Join Date
    Oct 2014
    Posts
    4
    Quote Originally Posted by alien1 View Post
    im also new to c but i think there is also a problem with this line
    Code:
    // look for a second number from user input scanf("%f", secondNumber);


    I think it should read
    Code:
    scanf( "%f" , & secondNumber );
    Thank-you so much~! It's such an obvious mistake I kept overlooking it, this is the first time it's actually bringing a calculated answer back! However it's bringing back an infinite number of them due to the loop :/ Will try to see about what Matticus said ^.^

  8. #8
    Registered User K.Aki's Avatar
    Join Date
    Oct 2014
    Posts
    4

    Red face It's a success!!

    My first programme since Hello World to successfully do what it was meant to!!

    Thank-you so much to everyone who commented on this post and helped me~!

    Here is the final working code for any other noobs struggling to build the same type of programme:

    Code:
    #include<stdio.h>
    #pragmawarning(disable: 4996) 
    
    int main( )
    {    
    // Allocate memory (define float number)
    float firstNumber=0.0;
    float secondNumber=0.0;
    char operation;
    
    while(1)
        {    
    // look for a first number from user input
        scanf("%f", &firstNumber);
    
    // look for an operation (+,-,*,/) from user input
        scanf("%c", &operation);
    
    // look for a second number from user input
    scanf("%f", &secondNumber);
    
    // upon user pressing enter, calculate (first number) (operation) (second number) and show user the calculated answer
    
    if(operation=='+')
    {
    printf ("%f", firstNumber + secondNumber);
        }
    
    if(operation=='-')
    {
    printf ("%f", firstNumber - secondNumber);
        }
    
    if(operation=='*')
        {
        printf ("%f", firstNumber * secondNumber);
        }
            
    if(operation=='/')
        {
    printf ("%f", firstNumber / secondNumber);
        }
    
        printf("\n");        
    
        } // end while
    
    } // end main

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hello world
    By d4xyjen in forum C Programming
    Replies: 2
    Last Post: 08-07-2009, 08:49 AM
  2. A new World
    By Nichonl in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2002, 10:44 PM
  3. hello world, please help
    By Unregistered in forum C++ Programming
    Replies: 12
    Last Post: 04-30-2002, 10:44 PM
  4. New to the World
    By oo0speed0oo in forum Game Programming
    Replies: 12
    Last Post: 04-04-2002, 10:46 AM
  5. Hello World in C#
    By Witch_King in forum A Brief History of Cprogramming.com
    Replies: 51
    Last Post: 08-24-2001, 02:09 PM

Tags for this Thread