Inputting values into a variable

This is a discussion on Inputting values into a variable within the C Programming forums, part of the General Programming Boards category; I have wrote the code below to try and get input from the keyboard and store the data into the ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    14

    Inputting values into a variable

    I have wrote the code below to try and get input from the keyboard and store the data into the variables day, month and year, however I am receiving a compile error which says
    declared parameter "year" is missing
    It also says this about the day and month variable. I am confused


    Code:
    ]#include <stdio.h>
    
    int main()
    int day, month, year;
    {
    	scanf("%d/%d/%d",&day,&month,&year);
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,855
    The body of a function (including local variable declarations) goes between the braces (and you need to return a value from main):
    Code:
    #include <stdio.h>
    
    int main()
    {
      int day, month, year;
      scanf("&#37;d/%d/%d",&day,&month,&year);
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,824
    I am not sure about the syntax, but I believe you are using a legacy syntax for declaring parameters. You actually want to write:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int day, month, year;
        scanf("&#37;d/%d/%d", &day, &month, &year);
    
        return 0; // Optional in C99.
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    14
    I'm confused why this one works and the other one does not, aren't they both the same?

  5. #5
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    you have to declare variables inside the main function.
    Code:
    #include <stdio.h>
    
    int main()
    int day, month, year;    /*This statement is outside main coz/*
    {                                   /*main is starting from here*/

    And if you want your variables to be global, put it like this:
    Code:
    #include <stdio.h>
    int day, month, year;   /*These are now global variables*/
    int main()
    {              /*Nothing comes between int main and this brace*/

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Both Preludes and Laserlights posts are the same. Yours is every so slightly different in that you have the variables BETWEEN the () of main and the { that begins the statements INSIDE the function main - this is where, in the old (original) C style, you'd place the argument types - but you also had to declare those in the () of the function, so:

    Code:
    int main(day, month, year)
    int day, month, year;
    {
    ...
    This would, technically, work, except that the function main is never called with three integer values as arguments. In C++ this would definitely be invalid, and it's on the "undefined" stage of C - so it may work on one system, and not work on another system.

    It is definitely not the right way to do things.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    Quote Originally Posted by matsp View Post
    Both Preludes and Laserlights posts are the same. Yours is every so slightly different in that you have the variables BETWEEN the () of main and the { that begins the statements INSIDE the function main - this is where, in the old (original) C style, you'd place the argument types - but you also had to declare those in the () of the function, so:

    Code:
    int main(day, month, year)
    int day, month, year;
    {
    ...
    --
    Mats
    It remembered me an old book ...
    Mac OS 10.6 Snow Leopard : Darwin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Controlling variable values by printing on file
    By p3rry in forum C Programming
    Replies: 8
    Last Post: 12-17-2008, 09:09 AM
  2. Storing int and double type values under one variable name
    By Thanuja91 in forum C++ Programming
    Replies: 10
    Last Post: 10-30-2007, 04:15 AM
  3. Replies: 1
    Last Post: 02-03-2005, 02:33 AM
  4. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 12:25 PM
  5. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM

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