5 Replies - 752 Views - Last Post: 15 October 2014 - 04:01 PM Rate Topic: -----

#1 BobbyS.  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 10
  • Joined: 15-October 14

use of undeclared identifier 'colours'

Posted 15 October 2014 - 12:53 PM

Okay I am fairly new to coding and I keep getting this error message when i try to build the program. Any help/advice would be greatly appreciated,Thank you for your time!
Also this is my first post and i don't know how to correctly post the code here.

#include <stdio.h>
#include <stdlib.h>

int main ()
{
    int opt;

    do
{
    printf("Please choose an option:\n");
    printf("1. Calculate resistance value\n");
    printf("2. Help\n");
    printf("3. Exit\n");

    scanf("%d", &opt);

    switch(opt)
    {
    case 1:
        char *colours[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; /*error message here (line20)*/

        int main( void );

int band1, band2, band3, tolerance, ok;

do {
printf("Enter 1st band [0-9] : ");
ok = scanf("%d", &band1);
} while (ok!=1 || band1<0 || band1>9);

do {
printf("Enter 2nd band [0-9] : ");
ok = scanf("%d", &band2);
} while (ok!=1 || band2<0 || band2>9);

do {
printf("Enter multiplier as an power of 10 [0-9] : ");
ok = scanf("%d", &band3);
} while (ok!=1 || band3<0 || band3>9);


do {
printf("Enter tolerance percentage: 5, 10, or 0 (none) : ");
ok = scanf("%d", &tolerance);
} while (ok!=1 || (tolerance!=5 && tolerance!=10 && tolerance!=0));

printf("The resistor has a value of: %s%sx10^%s", colours[band1], colours[band2], colours[band3]); /*error message here (line47)*/





The error messages that i am getting are : error: expected expression (line20)
: error: use of undeclared identifier 'colours' (line 47)

Any advice/help is needed!

This post has been edited by modi123_1: 15 October 2014 - 01:01 PM
Reason for edit:: please be descriptive in the title


Is This A Good Question/Topic? 0
  • +

Replies To: use of undeclared identifier 'colours'

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13488
  • View blog
  • Posts: 53,864
  • Joined: 12-June 08

Re: use of undeclared identifier 'colours'

Posted 15 October 2014 - 01:02 PM

22	        int main( void );

Why do you have this call to main here?

Where does you switch end?

I think you are missing a lot of ending curly braces
Was This Post Helpful? 1
  • +
  • -

#3 BobbyS.  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 10
  • Joined: 15-October 14

Re: use of undeclared identifier 'colours'

Posted 15 October 2014 - 01:19 PM

View Postmodi123_1, on 15 October 2014 - 01:02 PM, said:

22	        int main( void );

Why do you have this call to main here?

Where does you switch end?

I think you are missing a lot of ending curly braces


I honestly don't know why, i guess i can delete the main? Ill post the full code here, sorry for the confusion.

#include <stdio.h>
#include <stdlib.h>

int main ()
{
    int opt;

    do
{
    printf("Please choose an option:\n");
    printf("1. Calculate resistance value\n");
    printf("2. Help\n");
    printf("3. Exit\n");

    scanf("%d", &opt);

    switch(opt)
    {
    case 1:
        char *colours[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

        int main( void );

int band1, band2, band3, tolerance, ok;

do {
printf("Enter 1st band [0-9] : ");
ok = scanf("%d", &band1);
} while (ok!=1 || band1<0 || band1>9);

do {
printf("Enter 2nd band [0-9] : ");
ok = scanf("%d", &band2);
} while (ok!=1 || band2<0 || band2>9);

do {
printf("Enter multiplier as an power of 10 [0-9] : ");
ok = scanf("%d", &band3);
} while (ok!=1 || band3<0 || band3>9);


do {
printf("Enter tolerance percentage: 5, 10, or 0 (none) : ");
ok = scanf("%d", &tolerance);
} while (ok!=1 || (tolerance!=5 && tolerance!=10 && tolerance!=0));

printf("The resistor has a value of: %s%sx10^%s", colours[band1], colours[band2], colours[band3]);

if (tolerance==5)
printf("~0.05 Ohms");
else if (tolerance==10)
printf("~0.1 Ohms");
else if(tolerance==0)
    printf(" Ohms");

printf("\n\n");
break;


    case 2:

   printf( "\n\n\tThe colored bands are coded as follows:\n\n\n\t" );
   printf( "COLOR\t\t\tCODE\n\t" );
   printf( "-----\t\t\t----\n\n" );
   printf( "\tBlack------------------->0\n" );
   printf( "\tBrown------------------->1\n" );
   printf( "\tRed--------------------->2\n" );
   printf( "\tOrange------------------>3\n" );
   printf( "\tYellow------------------>4\n" );
   printf( "\tGreen------------------->5\n" );
   printf( "\tBlue-------------------->6\n" );
   printf( "\tViolet------------------>7\n" );
   printf( "\tGray-------------------->8\n" );
   printf( "\tWhite------------------->9\n" );
   printf( "\tGold-------------------->~5\n");
   printf( "\tSilver------------------>~10\n");
   break;


    case 3:
        break;
    default:
        printf("Your option is invalid, Please try again.\n");


    }
}while (opt!=1);
return 0;
}

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon

Reputation: 5334
  • View blog
  • Posts: 16,609
  • Joined: 25-December 09

Re: use of undeclared identifier 'colours'

Posted 15 October 2014 - 01:42 PM

Please post your complete error messages, exactly as they appear in your development environment.


You also need to find and use a consistent indentation style. This will make your program much easier to read.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 BobbyS.  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 10
  • Joined: 15-October 14

Re: use of undeclared identifier 'colours'

Posted 15 October 2014 - 02:24 PM

View Postjimblumberg, on 15 October 2014 - 01:42 PM, said:

Please post your complete error messages, exactly as they appear in your development environment.


You also need to find and use a consistent indentation style. This will make your program much easier to read.


Jim


Alright thank you, the complete error messages that i am getting are : error: expected expression
error: use of undeclared identifier 'colours'
#include <stdio.h>
#include <stdlib.h>

int main ()
{
    int opt;

    do
{
    printf("Please choose an option:\n");
    printf("1. Calculate resistance value\n");
    printf("2. Help\n");
    printf("3. Exit\n");

    scanf("%d", &opt);

    switch(opt)
    {
    case 1:
        char *colours[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };  /* error: expected expression*/

        int main( void );

int band1, band2, band3, tolerance, ok;

do {
printf("Enter 1st band [0-9] : ");
ok = scanf("%d", &band1);
} while (ok!=1 || band1<0 || band1>9);

do {
printf("Enter 2nd band [0-9] : ");
ok = scanf("%d", &band2);
} while (ok!=1 || band2<0 || band2>9);

do {
printf("Enter multiplier as an power of 10 [0-9] : ");
ok = scanf("%d", &band3);
} while (ok!=1 || band3<0 || band3>9);


do {
printf("Enter tolerance percentage: 5, 10, or 0 (none) : ");
ok = scanf("%d", &tolerance);
} while (ok!=1 || (tolerance!=5 && tolerance!=10 && tolerance!=0));

printf("The resistor has a value of: %s%sx10^%s", colours[band1], colours[band2], colours[band3]);  /*error: use of undeclared identifier 'colours'*/

if (tolerance==5)
printf("~0.05 Ohms");
else if (tolerance==10)
printf("~0.1 Ohms");
else if(tolerance==0)
    printf(" Ohms");

printf("\n\n");
break;


    case 2:

   printf( "\n\n\tThe colored bands are coded as follows:\n\n\n\t" );
   printf( "COLOR\t\t\tCODE\n\t" );
   printf( "-----\t\t\t----\n\n" );
   printf( "\tBlack------------------->0\n" );
   printf( "\tBrown------------------->1\n" );
   printf( "\tRed--------------------->2\n" );
   printf( "\tOrange------------------>3\n" );
   printf( "\tYellow------------------>4\n" );
   printf( "\tGreen------------------->5\n" );
   printf( "\tBlue-------------------->6\n" );
   printf( "\tViolet------------------>7\n" );
   printf( "\tGray-------------------->8\n" );
   printf( "\tWhite------------------->9\n" );
   printf( "\tGold-------------------->~5\n");
   printf( "\tSilver------------------>~10\n");
   break;


    case 3:
        break;
    default:
        printf("Your option is invalid, Please try again.\n");


    }
}while (opt!=1);
return 0;
}

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon

Reputation: 5334
  • View blog
  • Posts: 16,609
  • Joined: 25-December 09

Re: use of undeclared identifier 'colours'

Posted 15 October 2014 - 04:01 PM

Let's look at the following snippet:

      switch(opt)
      {
         case 1:
            char *colours[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };  /* error: expected exp<b></b>ression*/

            int main( void );

            int band1, band2, band3, tolerance, ok;



First you've already been told about the issue with the int main(void);. Remember main is special function, you don't prototype this function and you can't call this function from any function, including main().

Next a case statement doesn't create a block so you can't declare variables inside a case statement, unless you create a block by using braces.

      switch(opt)
      {
         case 1:
         {
            char *colours[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; 

            int band1, band2, band3, tolerance, ok;
...
         } // end of case 1



But really the best thing to do would be to use a function and place all the code, except the break statement, that is inside this case statement into the function.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1