wont compile due to warnings?

This is a discussion on wont compile due to warnings? within the C Programming forums, part of the General Programming Boards category; Im about to pull my hair out. So my task is to have the user input a number from 1 ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    4

    wont compile due to warnings?

    Im about to pull my hair out. So my task is to have the user input a number from 1 to 20 and I'm supposed to set up a long switch statement to take the number and output it as a roman numeral. I'm fairly new to C so go easy on me.

    The warnings I'm getting are in the switch statement from the second case where I try to change the roman char to something. I get an overflow in implicit constant conversion.

    Again, they're just warnings but the program wont let me compile. I guess I don't fully understand how the concept of strings in C. After looking up online stuff for strings in C, I'm still not sure why those wont work. Any help would be grand.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void){
        
        // variable
        int num;
        char roman = 0;
        
        // get input from user
        printf("Enter a number between 1 and 20: ");  
        scanf("%d", &num);
        
        // call method
        romanNumeral(num, roman);
        
        system("pause");
        return 0;
        
    } // end of main
    
    int romanNumeral(int num, char roman){
          
         
         // roman numeral switch statement
         switch(num){
              case 1:
                   roman = 'I';
                   break;
              case 2:
                   roman = 'II';
                   break;
              case 3:
                   roman = 'III';
                   break;
              case 4:
                   roman = 'IV';
                   break;
              case 5:
                   roman = 'V';
                   break;
              case 6:
                   roman = 'VI';
                   break;
              case 7:
                   roman = 'VII';
                   break;
              case 8:
                   roman = 'VIII';
                   break;
              case 9:
                   roman = 'IX';
                   break;
              case 10:
                   roman = 'X';
                   break;
              case 11:
                   roman = 'XI';
                   break;
              case 12:
                   roman = 'XII';
                   break;
              case 13:
                   roman = 'XIII';
                   break;
              case 14:
                   roman = 'XIV';
                   break;
              case 15:
                   roman = 'XV';
                   break;
              case 16:
                   roman = 'XVI';
                   break;
              case 17:
                   roman = 'XVII';
                   break;
              case 18:
                   roman = 'XVIII';
                   break;
              case 19:
                   roman = 'XIX';
                   break;
              case 20:
                   roman = 'XX';
                   break;
              default:
                   num = -1;
         } // end of switch
         
         // if/else statement to verify number
         if (num < 1 || num > 20){
            printf("Error: Invalid Number.");
         } else {
            printf("Roman Numeral %d: %s", num, roman);
         }
         
    } // end of function

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    One character is not enough space for even 1 through 10 in Roman numerals. You need to use a string.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    You also need to have at least the function's prototype appear before main in order to call it.

    That's unfortunate that they ask you to use a switch statement. It is possible to correctly convert to roman numberals input of up to 4000, in only 14 lines of code, including range checking.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    Sorry, I think I failed to mention that this is C and not C++. Every tutorial I went through doesn't have the String type, is that right?

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    Quote Originally Posted by iMalc View Post
    You also need to have at least the function's prototype appear before main in order to call it.

    That's unfortunate that they ask you to use a switch statement. It is possible to correctly convert to roman numberals input of up to 4000, in only 14 lines of code, including range checking.
    Same goes for this, do you need function prototypes in C?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Quote Originally Posted by arubix View Post
    Sorry, I think I failed to mention that this is C and not C++. Every tutorial I went through doesn't have the String type, is that right?
    You do not have to mention if c or c++.That's why the forum has a thread for c and another one for c++
    As for the string ,you can include the header string.h and go on..However i never did that on C,so instead of the header you have to include,you could try a pointer to char(you have to malloc the memory needed for the string you are going to store),or an array of char.
    Quote Originally Posted by arubix View Post
    Same goes for this, do you need function prototypes in C?
    Of course.You must have the prototype of the function,or the declaration of the function before you use it,thus before main function.

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    593
    Must declare function prototype in C? - Stack Overflow
    Well, it is not actually required to declare prototypes, but it is good practice.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Quote Originally Posted by camel-man View Post
    Must declare function prototype in C? - Stack Overflow
    Well, it is not actually required to declare prototypes, but it is good practice.
    I compiled this piece of code in a linux
    Code:
    int main(void)
    {
        bla();
        return 0;
    }
    
    void bla(void)
    {
            int a;
    }
    and got this error

    Code:
    linux05:/home/users/std10093>gcc -o pr pr.c
    pr.c:8: warning: conflicting types for 'bla'
    pr.c:3: warning: previous implicit declaration of 'bla' was here
    linux05:/home/users/std10093>
    Maybe new compilers(the one of this linux must be old) allow what the camel said,but i would suggest you always to declare the prototypes before main

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Quote Originally Posted by arubix View Post
    Same goes for this, do you need function prototypes in C?
    Depends what you mean by "need".

    If you don't provide a function declaration (the actual name of a "prototype") then older compilers will assume the function returns int, and assume nothing about its arguments.

    If your function, as implemented, actually returns something other than int, and your code attempts to use the return value, your code will exhibit undefined behaviour. With all versions of C.

    Your code will also exhibit undefined behaviour if there is an argument mismatch, such as the types not aligning, or passing two arguments to a function that expects three. The compiler cannot diagnose such things.

    The "feature" is deprecated in C, and most modern compilers will give a warning. Some modern C compilers will refuse to compile your code, particularly those that are actually C++ compilers.

    So, if all your functions actually return int, you only ever pass EXACTLY the types of arguments the function expects, if you are content to ignore compiler warnings, if you use a C compiler that is not a C++ compiler, if you plan never to upgrade your compiler, and you are content to have other programmers tell you you are living in the dark ages, then you don't need function prototypes.

    If any of those conditions are untrue, well ..... you decide.
    Last edited by grumpy; 09-03-2012 at 02:42 PM.
    Right 98% of the time, and don't care about the other 3%.

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    thank you all for the swift responses. I've adjusted my code by adding the function prototype and changing some things with the function. I turned it into void since I forgot to change it since I'm not going to return anything to my main method. I also added an array to char. The compiler shows everything is ok now but when I execute the program, any number other than 1-20 prompts me with an invalid number(which is great) but any number from 1 - 20 shows a bunch of jambled characters. Other than that tho, the program executes and ends without choking.

    Code:
    // Program that asks user to input a number 
    // between 1 - 20 and the output results in 
    // a roman numeral of the input.
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void romanNumeral(int num);
    
    int main(void){
        
        // variable
        int num;
        
        // get input from user
        printf("Enter a number between 1 and 20: ");  
        scanf("%d", &num);
        
        // call method
        romanNumeral(num);
        
        system("pause");
        return 0;
        
    } // end of main
    
    void romanNumeral(int num){
          
         char roman[6];
         
         // roman numeral switch statement
         switch(num){
              case 1:
                   roman[6] = 'I';
                   break;
              case 2:
                   roman[6] = 'II';
                   break;
              case 3:
                   roman[6] = 'III';
                   break;
              case 4:
                   roman[6] = 'IV';
                   break;
              case 5:
                   roman[6] = 'V';
                   break;
              case 6:
                   roman[6] = 'VI';
                   break;
              case 7:
                   roman[6] = 'VII';
                   break;
              case 8:
                   roman[6] = 'VIII';
                   break;
              case 9:
                   roman[6] = 'IX';
                   break;
              case 10:
                   roman[6] = 'X';
                   break;
              case 11:
                   roman[6] = 'XI';
                   break;
              case 12:
                   roman[6] = 'XII';
                   break;
              case 13:
                   roman[6] = 'XIII';
                   break;
              case 14:
                   roman[6] = 'XIV';
                   break;
              case 15:
                   roman[6] = 'XV';
                   break;
              case 16:
                   roman[6] = 'XVI';
                   break;
              case 17:
                   roman[6] = 'XVII';
                   break;
              case 18:
                   roman[6] = 'XVIII';
                   break;
              case 19:
                   roman[6] = 'XIX';
                   break;
              case 20:
                   roman[6] = 'XX';
                   break;
              default:
                   num = -1;
         } // end of switch
         
         // if/else statement to verify number
         if (num < 1 || num > 20){
            printf("\nError: Invalid Number.\n\n");
         } else {
            printf("\nRoman Numeral %d: %s\n\n", num, roman);
         }
         
    } // end of function

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Remember where counting for arrays begin...If you have an int array[n] for example...then the first element is the array[0],the second the array[1]..and so on...and the last element is array[n-1]
    So you have a char roman[6] and you type roman[6]='I' .You are lucky that you did not receive a segmentation fault.If you run it many times you actually might get one,because you are trying to access memory that is not what you think it is(you think that this is the last element of your array but actually it is something else!)

    However you should also mind that when we want a string to be put into an array we can use easily the strcpy function strcpy - C++ Reference

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why wont this compile:
    By ItchyBob in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2011, 12:25 PM
  2. Why wont this compile?
    By Dieselfreak in forum C Programming
    Replies: 5
    Last Post: 12-13-2010, 10:37 AM
  3. reinterpret_cast compile warnings
    By deadpoet in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2004, 06:06 PM
  4. Why wont anything compile?
    By Crazyflanger in forum C++ Programming
    Replies: 7
    Last Post: 12-14-2003, 01:18 AM
  5. Wont Compile In VS.NET
    By Troll_King in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 AM

Tags for this Thread


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