Thread: Help with errors

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    Help with errors

    Code:
    #include <stdio.h>
    #include <string.h>
    #define NOT_FOUND -1
    
    int
    search(const char *arr[][7],const char *target[],int n);
    
    int
    main(void)
    {
    char c1[10], c2[10], c3[10];
    int x,y,z;
    double resistance;
    
    char ColorArray[10][7] =
    {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet",
    "gray", "white"};
    
    do {
    printf("Enter the colors of the resistor's three bands, ");
    printf("beginning with the band nearest the end. ");
    printf("Type the colors in lowercase letters only, NO CAPS. ");
    
    
    printf("Band 1=> ");
    scanf("%c", c1);
    x = search(ColorArray, c1, 10);
    
    printf("Band 2=> ");
    scanf("%c", c2);
    y = search(ColorArray, c2, 10);
    
    printf("Band 3=> ");
    scanf("%c", c3);
    z = search(ColorArray, c3, 10);
    
    
    resistance=((x*10)+y) * pow(10, z);
    
    
    
    printf("Resistance Value>%f\n", resistance);
    printf(\n"Do you want to decode another resistor?\n");
    printf("=> ");
    scanf("%c", &again);
    } while (again == 'y' || again == 'Y');
    
    return (0);
    
    }
    
    int
    search(const char *arr[][7],const char *target[],int n);
    
    {
    int i,
    found=0
    where;
    i=0;
    while (!found && i < n){
            if(strcmp(arr[i], target) == 0)
                    found = 1;
            else
                    ++i;
            }
            if (found)
                    where=i;
            else
                    where = NOT_FOUND;
            return (where);
    }
    We're trying to compile our program, but are coming up with several errors. Does anyone have suggestions?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What are the errors?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by sublime
    We're trying to compile our program, but are coming up with several errors. Does anyone have suggestions?
    Yes, read the errors, look at the line they're associated with, and try to understand them. Most of the errors in your program are quite obvious and self-explanatory.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes, alot of syntax errors and undeclared errors. I'd also add that rather than forcing the user to enter in lowercase, you can just use tolower() from ctype.h
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    heres the errors. sorry about that. we're supposed to tell the user to use lower case. its part of the instructions.

    Code:
    hw5.c: In function `main':
    hw5.c:33: warning: passing arg 1 of `search' from incompatible pointer type
    hw5.c:33: warning: passing arg 2 of `search' from incompatible pointer type
    hw5.c:37: warning: passing arg 1 of `search' from incompatible pointer type
    hw5.c:37: warning: passing arg 2 of `search' from incompatible pointer type
    hw5.c:41: warning: passing arg 1 of `search' from incompatible pointer type
    hw5.c:41: warning: passing arg 2 of `search' from incompatible pointer type
    hw5.c:49: error: stray '\' in program
    hw5.c:49: error: `n' undeclared (first use in this function)
    hw5.c:49: error: (Each undeclared identifier is reported only once
    hw5.c:49: error: for each function it appears in.)
    hw5.c:49: error: parse error before string constant
    hw5.c:51: error: `again' undeclared (first use in this function)
    hw5.c: At top level:
    hw5.c:62: error: parse error before '{' token
    hw5.c:66: warning: data definition has no type or storage class
    hw5.c:67: error: parse error before "while"

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    [CODE]printf(\n"Do you want to decode another resistor?\n");[\CODE]
    the \n goes inside the "" marks
    [CODE]printf("\nDo you want to decode another resistor?\n");[\CODE]
    that's one less

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    scanf("%c", &again);

    YOU never declared again,

    search(const char *arr[][7],const char *target[],int n);
    that ; symbol should not go there

    delete it and leave only the {

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    yeah with the clear head now i fixed most but after i removed the ; after the search(const... i now have these for errors:

    Code:
    hw5.c: In function `main':
    hw5.c:28: warning: passing arg 1 of `search' from incompatible pointer type
    hw5.c:28: warning: passing arg 2 of `search' from incompatible pointer type
    hw5.c:32: warning: passing arg 1 of `search' from incompatible pointer type
    hw5.c:32: warning: passing arg 2 of `search' from incompatible pointer type
    hw5.c:36: warning: passing arg 1 of `search' from incompatible pointer type
    hw5.c:36: warning: passing arg 2 of `search' from incompatible pointer type
    hw5.c: In function `search':
    hw5.c:62: warning: passing arg 1 of `strcmp' from incompatible pointer type
    hw5.c:62: warning: passing arg 2 of `strcmp' from incompatible pointer type
    Undefined                       first referenced
     symbol                             in file
    pow                                 /var/tmp//cciBCqkV.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status
    thanks a lot for your help.

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    Code:
    int i,
    found=0         //A colon is missing here ","
    where;
    Code:
    int i=0, found=0, where;

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    found=0         //A colon is missing here ","
    "," is a colon?

    pow() is in <math.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    alright this is the revised coding. still cant get the pow function to work after including math.h. i realize im asking a lot of you guys but honestly if you dont want to help dont worry about it. its no big deal.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #define NOT_FOUND -1
    
    int
    search(const char *arr[][7], const char *target[], int n);
    
    int
    main(void)
    {
    char c1[10], c2[10], c3[10];
    int x,y,z;
    double resistance;
    char again;
    
    char ColorArray[10][7] =
    {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet",
    "gray", "white"};
    
    do {
    printf("Enter the colors of the resistor's three bands, ");
    printf("beginning with the band nearest the end. ");
    printf("Type the colors in lowercase letters only, NO CAPS. ");
    
    
    printf("Band 1=> ");
    scanf("%c", c1);
    x = search(ColorArray, c1, 10);
    
    printf("Band 2=> ");
    scanf("%c", c2);
    y = search(ColorArray, c2, 10);
    
    printf("Band 3=> ");
    scanf("%c", c3);
    z = search(ColorArray, c3, 10);
    
    
    resistance=((x*10)+y) * pow(10, z);
    
    
    printf("Resistance Value>%f\n", resistance);
    printf("\nDo you want to decode another resistor?(y/n)\n");
    printf("=> ");
    scanf("%c", &again);
    } while (again == 'y' || again == 'Y');
    
    return (0);
    
    }
    
    int
    search(const char *arr[][7], const char *target[], int n)
    
    {
    int i, found = 0, where;
    
    i = 0;
    while (!found && i < n) {
            if (strcmp(arr[i], target) == 0)
    found = 1;
            else
                    ++i;
            }
            if (found)
                    where = i;
            else
                    where = NOT_FOUND;
            return (where);
    }
    
    
    hw5.c: In function `main':
    hw5.c:29: warning: passing arg 1 of `search' from incompatible pointer type
    hw5.c:29: warning: passing arg 2 of `search' from incompatible pointer type
    hw5.c:33: warning: passing arg 1 of `search' from incompatible pointer type
    hw5.c:33: warning: passing arg 2 of `search' from incompatible pointer type
    hw5.c:37: warning: passing arg 1 of `search' from incompatible pointer type
    hw5.c:37: warning: passing arg 2 of `search' from incompatible pointer type
    hw5.c: In function `search':
    hw5.c:61: warning: passing arg 1 of `strcmp' from incompatible pointer type
    hw5.c:61: warning: passing arg 2 of `strcmp' from incompatible pointer type
    Undefined                       first referenced
     symbol                             in file
    pow                                 /var/tmp//ccIdtz6H.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, you never need to pass pointers to arrays if you're just trying to get access to an array's contents [to use in] inside another function. All you do is pass the name of the array, and make the arguments be the same as the array.

    However...

    You'll want to read this FAQ, because you can't just pass multiple dimension arrays around between functions...


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. Help me with these errors... :-(
    By major_small in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2003, 08:18 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM