Search code examples
c++lowercase

Error ~ Check if character is lowercase letter


This is the code that I had written to check whether [i] is a lowercase letter or not.

    #include<iostream>
    #include<conio.h>
    #include<stdio.h>
    using namespace std;
    int main()
    {
       int i=0;
       char str[i]="Enter an alphabet:";
       char i;
       while(str[i])
            {
              i=str[i];
              if (islower(i))  i=toupper(i);
              putchar(i);
              i++;
            }
       return 0;
     }

The Error I m getting is

    ||=== Build: Debug in practice (compiler: GNU GCC Compiler) ===|
    C:\Users\Public\Documents\krish\practice\main.cpp||In function 'int main()':|
    C:\Users\Public\Documents\krish\practice\main.cpp|9|error: conflicting declaration 'char i'|
    C:\Users\Public\Documents\krish\practice\main.cpp|7|note: previous declaration as 'int i'|
    ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Solution

  • The problem is exactly what the error message says: You declare i twice. Once as char and once as int.

    int i=0; // declare i as int and assign 0
    char str[i]="Enter an alphabet:";
    char i; // declare i as char -> i is already declared as int.
    

    Rename one of your variables.

    Also don't use conio.h - it's not part of the standard C library nor is it defined by POSIX.