Thread: Help writing program

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    8

    Help writing program

    Can someone please help me write these 2 little programs, i been having trouble understanding how to get it done.

    1. Write a program that requests three numbers from the user and prints out whether the numbers are the same or not. Your code may contain only one if statement.
    Please enter number 1: 3
    Please enter number 2: 4
    Please enter number 3: 3
    The numbers ARE NOT the same


    Please enter number 1: 3
    Please enter number 2: 3
    Please enter number 3: 3
    The numbers ARE the same


    2. Write a program that requests three numbers from the user and prints out whether at least two of the numbers are the same or not. Your code may contain only one if statement .
    Please enter number 1: 3
    Please enter number 2: 4
    Please enter number 3: 3
    At least two of the numbers are the same


    Please enter number 1: 2
    Please enter number 2: 3
    Please enter number 3: 4
    None of the numbers are the same



    I would appreciate any kind of help getting these done
    Last edited by grjae; 03-24-2011 at 07:13 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You may want to read homework policy first.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    Thanks, I will post my code i got done and see if anyone can help me with my proplems

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You are limited to one 'if' statement, so it will contain a bunch of something compared with something OR/AND something compared with something AND/OR something compared with something. You will have to understand how to do compares, "AND", "OR".

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    This is the code i came up with, can anyone help me fix the problems and help me understand what i did wrong?
    Code:
    #include "StdAfx.h"
    #include <iostream>
    
    {
    cout<< "Please enter number1";
    cin>> Number1;
    cout<< "Please enter number2;
    cin>> Number2;
    cout<< "Please enter number3;
    cin>> Number 3;
    
    
    int Number1, Number2, Number3;
    if (Number1 == Number2 && Number2 == Number3)
    
    cout<< the numbers are not the same;
    }
    else;
    {
    cout<< the numbers are the same;
    }
    return 0;

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You can't use variables before you declare them.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by grjae View Post
    This is the code i came up with, can anyone help me fix the problems and help me understand what i did wrong?
    Code:
    #include "StdAfx.h"
    #include <iostream>
    
    {
    cout<< "Please enter number1";
    cin>> Number1;
    cout<< "Please enter number2;
    cin>> Number2;
    cout<< "Please enter number3;
    cin>> Number 3;
    
    
    int Number1, Number2, Number3;
    if (Number1 == Number2 && Number2 == Number3)
    
    cout<< the numbers are not the same;
    }
    else;
    {
    cout<< the numbers are the same;
    }
    return 0;

    That's C++ code... not C ... You've got the concept... just wrote it in the wrong language.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Actually if you checked if (Number1 == Number2 && Number2 == Number3), all the numbers ARE the same.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    I fixed the problems but still cant get it to run. These are the errors I am getting
    Code:
    c:\documents and settings\kathy\my documents\visual studio 2010\projects\g\g\g.cpp(7): error C2447: '{' : missing function header (old-style formal list?)
    c:\documents and settings\kathy\my documents\visual studio 2010\projects\g\g\g.cpp(21): error C2059: syntax error : 'else'
    c:\documents and settings\kathy\my documents\visual studio 2010\projects\g\g\g.cpp(22): error C2447: '{' : missing function header (old-style formal list?)
    c:\documents and settings\kathy\my documents\visual studio 2010\projects\g\g\g.cpp(25): error C2059: syntax error : 'return'

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Where is main(...) ?
    The if statement should have opening brace. since you have a closed one.
    The else should not have a semicolon immediately following it.
    You are missing a closing brace after the return statement - which is the end of the function. There is no function header though.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by grjae View Post
    I fixed the problems but still cant get it to run. These are the errors I am getting
    Code:
    c:\documents and settings\kathy\my documents\visual studio 2010\projects\g\g\g.cpp(7): error C2447: '{' : missing function header (old-style formal list?)
    c:\documents and settings\kathy\my documents\visual studio 2010\projects\g\g\g.cpp(21): error C2059: syntax error : 'else'
    c:\documents and settings\kathy\my documents\visual studio 2010\projects\g\g\g.cpp(22): error C2447: '{' : missing function header (old-style formal list?)
    c:\documents and settings\kathy\my documents\visual studio 2010\projects\g\g\g.cpp(25): error C2059: syntax error : 'return'
    Post the updated code.
    bit∙hub [bit-huhb] n. A source and destination for information.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... what you've got there is not even a complete program... At bare minimum a C program needs...
    Code:
    int main (void)
      {  
    
          // add your code here
    
       return 0; }
    None of which you have.

    Also you are writing a hodgepodge of C++ and C ... cout and cin are C++ not C....

    You need to read up on program structure...
    You need to pick a language...

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    This is the way my teacher has been teaching us for my C programming class. I only have been in the class for two weeks so I am still very new with how to put it in the right structure. This is the code i got now.
    Code:
    #include "StdAfx.h"
    #include <iostream>
    
    
    
    
    int main();
    int Number1, Number2, Number3;
    {
    cout<< "Please enter number1";
    cin>> Number1;
    cout<< "Please enter number2";
    cin>> Number2;
    cout<< "Please enter number3";
    cin>> Number3;
    
    
    if (Number1 == Number2 && Number2 == Number3)
    
    cout<< the numbers ARE the same;
    }
    else
    {
    cout<< the numbers ARE NOT the same;
    
    return 0;
    }

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by grjae View Post
    This is the way my teacher has been teaching us for my C programming class. I only have been in the class for two weeks so I am still very new with how to put it in the right structure. This is the code i got now.
    HOLY CRAP! Your teacher is teaching this hodgepodge as C language?
    You need to sit down with the Dean or Principal and start the conversaition with: "Excuse me sir, but I don't think my C programming teacher knows what he's doing..."



    Code:
    #include "StdAfx.h"    << --- not a C header
    #include <iostream>  << --- Not a C header
    
    int main();                                          <<--- should be int main (void)
    int Number1, Number2, Number3;     << --- old style pre C-89 variable declarations
    {
    cout<< "Please enter number1";       <<--- cout is NOT a C function
    cin>> Number1;                                  <<--- cin is NOT a C function
    cout<< "Please enter number2";
    cin>> Number2;
    cout<< "Please enter number3";
    cin>> Number3;
    
    
    if (Number1 == Number2 && Number2 == Number3)  <<-- well, this much is right.
    
    cout<< the numbers ARE the same;  << --- not a C function,  should be quoted in C++
    }                                                      <<-- mismatched braces
    else
    {                                                      <<-- more mismatched braces 
    cout<< the numbers ARE NOT the same;   <<-- not a C function, should be quoted in C++
    
    return 0;
    }
    Now take this to your teacher's boss... and get yourself a proper teacher!
    Last edited by CommonTater; 03-25-2011 at 01:43 PM.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int main();                                          <<--- should be int main (void)
    int Number1, Number2, Number3;     << --- old style pre C-89 variable declarations
    {
    I think he just goofed there. If it was old style declarations as arguments for main, the parenthesis of main would look like:
    Code:
    int main(Number1,Number2,Number3)
    Also, he has a ; at the end of main() which makes it just a function prototype, which means that the { doesn't actually belong to any function.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems installing Mingw5.1.4
    By BlackOps in forum C Programming
    Replies: 2
    Last Post: 07-26-2009, 03:28 AM
  2. Writing a program to make a calendar
    By Northstar in forum C Programming
    Replies: 17
    Last Post: 11-07-2007, 11:34 AM
  3. writing a calendar program help please!!!
    By Newbie2006 in forum C Programming
    Replies: 7
    Last Post: 11-20-2002, 07:36 PM
  4. Help with a file writing program
    By pritesh in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 01:56 AM