What a great site! I'm looking fo a hint. I'm new to any type of programming and this C code just doesn't seem to be working.
I have attempted a program that gives the ohm value of a resistor when the color code is entered in. I am using an "enum" data type to assign a number value to each color code. The program will run but after I enter the first code, it jumps to the end and displays a nonsense answer. I'm just looking for a hint. I'm sure my mistake is obvios, just not to me. Any help will be great.

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

typedef enum {black, brown, red, orange, yellow, green, blue, violet, gray, white} color_code_t;


int main()
{
    int x;
    int y;
    double z;

  double res_value = 0;

  printf("...more explanation goes here, later...\n\n\n");

    printf("black is %d\n", black);
    printf("brown is %d\n", brown);
    printf("red is %d\n", red);
    printf("orange is %d\n", orange);
    printf("yelllow is %d\n", yellow);
    printf("greeen is %d\n", gray);
    printf("blue is %d\n", blue);
    printf("violet is %d\n", violet);
    printf("gray is %d\n", gray);
    printf("white is %d\n", white);

    /*get input, one color at a time*/

    printf("\t\t What color is the first band?\n=>");
    scanf("%d", &x);

    printf("\t\t What color is the second band?\n =>");
    scanf("%d", &y);

    printf("\t\t What color is the third band?\n=>");
    scanf("%lf", &z);

    /*the math*/

    res_value = ((x * 10) + y) * pow(10,z);


    printf("\t\t The resistor value is %f ohms.\n",  res_value);

    return 0;
}

A couple questions and a statement.
Is your compiler or you linking the math library for the pow() function?

What do you enter at the first prompt? A number (integer) or the label? Do you enter `blue' or do you enter `6'?

Here's the statement. You need to uncomment the stdio.h header file if you want printf(), and scanf() to work.

A couple questions and a statement.
Is your compiler or you linking the math library for the pow() function?

What do you enter at the first prompt? A number (integer) or the label? Do you enter `blue' or do you enter `6'?

Here's the statement. You need to uncomment the stdio.h header file if you want printf(), and scanf() to work.

Thank you for helping. The "//" came over by mistake. My biggest mistake is(please forgive my ignorance) I don't fully understand your answer. I thought including the math.h would let us use the power function. I'm reworking the program and re-reading the chapter. I'll post it again after some changes. Thanx again.

Thank you for helping. The "//" came over by mistake. My biggest mistake is(please forgive my ignorance) I don't fully understand your answer. I thought including the math.h would let us use the power function. I'm reworking the program and re-reading the chapter. I'll post it again after some changes. Thanx again.

What nonsense answer are you getting? I'm getting good answers, at least I think I am (see attachment).

Including math.h only takes care of the compiling part of building a program. It still needs to link using -lm with the gcc command. See this link.

But if you were able to run the program, it must have been created, which must mean it linked OK.

Like I said, it seemed to run fine for me (I compiled using Code Blocks, which linked math.h for me).

Like I said, it seemed to run fine for me (I compiled using Code Blocks, which linked math.h for me).

Most likely the nonsense is due to the input received by scanf() containing chars type. In any case when scanf() fails garbage are in those variables, and that's what gets computed and printed.

What nonsense answer are you getting? I'm getting good answers, at least I think I am (see attachment).

Including math.h only takes care of the compiling part of building a program. It still needs to link using -lm with the gcc command. See this link.

But if you were able to run the program, it must have been created, which must mean it linked OK.

Like I said, it seemed to run fine for me (I compiled using Code Blocks, which linked math.h for me).

Getting closer. My goal is for the program to eccept the color spelled out and convert that to a numeric value. For example red, brown green should produce 2,100,000. I've made some change but I think the proublem is the data types(maybee) This is what I have now;

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

typedef enum {black, brown, red, orange, yellow, green, blue, violet, gray, white} color_code_t;


int main()
{
    int x;
    int y;
    double z;

  double res_value = 0;

  printf("each colored band on a resistor has numeric value.\n---see below---\n\nCarefully spell out each of the first three colors\n of your resistor when prompted.\n\n\n");

    printf("black is %d\n", black);
    printf("brown is %d\n", brown);
    printf("red is %d\n", red);
    printf("orange is %d\n", orange);
    printf("yelllow is %d\n", yellow);
    printf("greeen is %d\n", gray);
    printf("blue is %d\n", blue);
    printf("violet is %d\n", violet);
    printf("gray is %d\n", gray);
    printf("white is %d\n", white);

    /*get input, one color at a time*/

    printf("\t\t spell the color of the fist band?\n  =>");
    scanf("%d", &x);

    printf("\t\t spell the color of the second band?\n  =>");
    scanf("%d", &y);

    printf("\t\t spell the color of the third band?\n  =>");
    scanf("%lf", &z);

    /*the math*/

    res_value = ((x * 10) + y) * pow(10,z);


    printf("\t\t The resistor value is %.4f ohms.\n",  res_value);

    return 0;
}

[...] My goal is for the program to [a]ccept the color spelled out and convert that to a numeric value.

Bingo!

Most likely the nonsense is due to the input received by scanf() containing chars type. In any case when scanf() fails garbage are in those variables, and that's what gets computed and printed.

Sorry, buddy! It doesn't work like that. And scanf() is not your friendly function to get string input. You are asking scanf to read some integers not some names.

Bingo!

Sorry, buddy! It doesn't work like that. And scanf() is not your friendly function to get string input. You are asking scanf to read some integers not some names.

Thats it! Thant what I needed to know. Thanx.

Thats it! Thant what I needed to know. Thanx.

Don't get discouraged! Keep trying!
To read string input (text, word, etc...) from user the function you can try is fgets()
example

char answer[32];
fgets( answer, sizeof answer, stdin );

A caveat with getting input is the `ENTER' key which is returned to any function like a new-line char ('\n'). fgets() includes it if there's room in the buffer.
Removing it is a must if you need to compare with some other string.
A common way of removing it might be

#include <string.h>
if ( fgets( answer, sizeof answer, stdin ) != NULL ) {
     size_t len = strlen ( answer );
     if ( answer[len -1] == '\n' ) {
         answer[len -1] = '\0';
     }
}

Why Am I telling you all this? Because, you need to compare strings at some point. strcmp() is your function for it.

typedef enum {black, brown, red, orange, yellow, green, blue, violet, gray, white} color_code_t;

The name of those colors are not real strings, there are numbers, integers camouflaged in a layer of abstraction so you don't have to deal with the number per se.

Don't get discouraged! Keep trying!
To read string input (text, word, etc...) from user the function you can try is fgets()
example

char answer[32];
fgets( answer, sizeof answer, stdin );

A caveat with getting input is the `ENTER' key which is returned to any function like a new-line char ('\n'). fgets() includes it if there's room in the buffer.
Removing it is a must if you need to compare with some other string.
A common way of removing it might be

#include <string.h>
if ( fgets( answer, sizeof answer, stdin ) != NULL ) {
     size_t len = strlen ( answer );
     if ( answer[len -1] == '\n' ) {
         answer[len -1] = '\0';
     }
}

Why Am I telling you all this? Because, you need to compare strings at some point. strcmp() is your function for it.

typedef enum {black, brown, red, orange, yellow, green, blue, violet, gray, white} color_code_t;

The name of those colors are not real strings, there are numbers, integers camouflaged in a layer of abstraction so you don't have to deal with the number per se.

You’re answer makes sense…but I had to jump ahead four chapters to find it in my text book. I think I’m getting ahead of myself. According to the syllabus, we’re not even going to cover those chapters. I’m still going to do this one but It will take me a day or two to thoroughly read up. (This book reads like the ingredients list of a Hostess product.) I WILL make this one work! …and I do appreciate you’re help. thanx

don't get hung up on the details of fgets() right now if it causes you to get too far ahead of your class instruction. to use it without understanding it could get you in trouble with your instructor that they might accuse you of plagiarizing someone else's solution.

if your instructor is expecting you to use scanf(), you can use it to get a string input like so: scanf("%s", color1); .... with one big "however":

most programmers don't recommend using scanf() for getting string input -- it's very prone to having all sort of bad behavior. I'm one of those people who dislike scanf(). i don't think it should be taught to new students at all.

but it is. and if it helps you to move past the string input issue for now, and concentrate on getting "the rest of your program" working... then go for it.

just be advised, what AIA posted about fgets() is the recommended way to do it, as it offers the most safety for the least cost.


.

What nonsense answer are you getting? I'm getting good answers, at least I think I am (see attachment).

Including math.h only takes care of the compiling part of building a program. It still needs to link using -lm with the gcc command. See this link.

But if you were able to run the program, it must have been created, which must mean it linked OK.

Like I said, it seemed to run fine for me (I compiled using Code Blocks, which linked math.h for me).

I think you discoverd the root of the problem. Nothing is wrong with the program. I think the purpose of the typedef enum is to eccept the input in the form of numbers (int). if I write the instruction "enter the number equivilant of the color..." the program does exactly what I asked it to do. (who knew?) I did learn a lot from this though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.