Thread: function problem

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Question function problem

    From the input file (mine is called ch10b) containing the following (the first digit representing the number sunny days
    in a month, the second the number of rainy days):

    4 9
    3 12
    6 12
    8 8
    12 5
    The following output is to sent to the screen:

    Weather Chart

    Month 1
    ****
    !!!!!!!!!

    Month 2
    ***
    !!!!!!!!!!!!

    etc. through to month 5. Basically, the program converts the relevant int. to a char (* for sunny days, ! for rainy)
    The spec for the program is to use a funtion called drawaline with 2 formal parameters of int length and char character.
    I'm getting error message 'type mismatch in redirection of 'drawline[int,char]'. If anyone can shed light on this I'd be
    very grateful!.

    So far I have done:

    #include<stdio.h>
    #include<conio.h>

    int length,
    drawalinea,
    drawalineb,
    sunny_day,
    rainy_day,
    month;

    char charactera = '*',
    characterb = '!';

    main()
    {

    FILE *ch10b = fopen("ch10b.dat", "r");
    printf("Weather Chart \n\n");
    month = 1;

    char drawaline(int, char);

    while(month <= 5)
    {
    fscanf(ch10b, "%d %d", &sunny_day, &rainy_day);
    printf("Month %d", month);
    drawalinea = drawaline(sunny_day, charactera);
    drawalineb = drawaline(rainy_day, characterb);
    printf("\n");
    month ++;
    }

    fclose(ch10b);
    getch();
    }

    drawaline(int length, char character)
    {
    int position = 1;
    while (position <= length)
    {
    printf("%c", character);
    position ++;
    }
    printf("\n");
    }

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    The 'type mistmatch' error message pretty much just says, "hey, you are trying to assign this type to this other type and it can't be done." Your problem is here:

    >> drawalinea = drawaline(sunny_day, charactera);
    >> drawalineb = drawaline(rainy_day, characterb);

    What types are drawalinea and drawalineb? They are ints. What type does drawaline return? A char. That is illegal (hence, the error). What you are going to have to do is make the return compatable with the value being assigned the return value.

    Understand?

    --Garfield
    1978 Silver Anniversary Corvette

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Angry Still stuck!?!

    Thanks for replying; I have changed the int to char for both the variables you mentioned, unfortunately I'm getting the same error message?

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Okay, I think I've got your problem. Right here:

    >> drawaline(int length, char character)

    This is the beginning (the declaration) of you function. You haven't written char or int (whichever you are using). The C compilier assumes that with nothing, you mean void. So, then it returns no value. Then you have this:

    >> drawalinea = drawaline(sunny_day, charactera);
    >> drawalineb = drawaline(rainy_day, characterb);

    If the functions return no value, you are going to get an error to assign from a no value return. So, you would have to change this to:

    drawaline(sunny_day, charactera);
    drawaline(rainy_day, characterb);

    I don't see the need to assign anything (whether it has a value or not) to those vars. They don't do anything. Then you should change your function prototype to:

    void drawaline(int, char);

    Tell me if this works. It should.

    --Garfield
    1978 Silver Anniversary Corvette

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Angry ARRRGGGGHHHHH!

    This is driving me nuts!! I'm still getting a redeclaration error..... here is my latest code (thanks for the help, it's really useful; I'm still new to C).

    #include<stdio.h>
    #include<conio.h>

    int length,
    sunny_day,
    rainy_day,
    month;

    char charactera = '*',
    characterb = '!';

    void drawaline(int, char);

    main()
    {

    FILE *ch10b = fopen("ch10b.dat", "r");
    printf("Weather Chart \n\n");
    month = 1;

    while(month <= 5)
    {
    fscanf(ch10b, "%d %d", &sunny_day, &rainy_day);
    printf("Month %d", month);
    drawaline(sunny_day, charactera);
    drawaline(rainy_day, characterb);
    printf("\n");
    month ++;
    }

    fclose(ch10b);
    getch();
    }

    drawaline(int length, char character)
    {
    int position = 1;
    while (position <= length)
    {
    printf("%c", character);
    position ++;
    }
    printf("\n");
    }

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Okay, I'm going to copy and paste this code and I'm going to go compile, see if it works, and fix it.

    I'll be back in a little bit.

    --Garfield
    1978 Silver Anniversary Corvette

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    You declared void on the declaration of "drawaline(int , char)"

    put void on the front of the fuction when you use is


    void drawaline(int length, char character)
    {
    int position = 1;
    while (position <= length)
    {
    printf("%c", character);
    position ++;
    }
    printf("\n");
    }

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    JFK, here is the working code. It compiled perfectly:
    Code:
    #include <stdio.h>
    
    void drawline(int length, char character);
    
    int main(int argc, char *argv[])
    {
    	FILE *fp;
    	char *valid, line[20];
    	int sun=0, rain=0, month=1;
    
    	fp = fopen(argv[1], "r");
    	if (fp == NULL)
    	{
    		printf("Put txt file in exe dir\n");
    		return 0;
    	}
    
    	valid = fgets(line, sizeof(line), fp);
    
    	while (valid != NULL)
    	{
    		sscanf(line, "%d %d", &sun, &rain);
    		printf("Month %d:\n", month);
    		drawline(sun, '*');
    		drawline(rain, '!');
    		month++;
    		valid = fgets(line, sizeof(line), fp);
    	}
    	return 0;
    }
    
    void drawline(int length, char character)
    {
    	int i=0;
    
    	while (i < length)
    	{
    		printf("%c", character);
    		i++;
    	}
    
    	printf("\n");
    }
    Okay, a couple of changes I made. Instead of the program choosing the file, I just used *arcv[] and retreived the file from that. So, to run the program, you would do:

    C:\Program Files:>Weather.exe <txt file here>

    Understand?

    --Garfield
    1978 Silver Anniversary Corvette

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Question Not really.....

    Thanks for the response. I've looked at the code and I have pasted it into Borland 3 but I'm not sure how to run it.

    Also, what is the '*argv[] ' syntax all about!? I haven't got as far as using that yet?

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Well

    argv[] is an array containing the parameters when your program starts. argc is the number of parameters passed.

    The first parameter (argv[0]) is the name of the program itself. If you run a program with this command (from a DOS prompt):

    MyProg.exe 365 Hello

    MyProg.exe is the first parameter (argv[0]), 365 is the second (argv[1]) and Hello is the third (argv[2]). This is very useful if you want to make your program more dynamic.

    Another cool feature with this is that you can associate extensions (ie *.BMP, *.TXT, *.DOC or whatever) with your program. If you double-click on that file, the name and search path of that file will be passed as the second parameter to your program.
    Now you just have to double-click on the file you want to open with your program instead of typing in the path to it every time you run it.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM