resistor colour band

closed account (Nh79E3v7)
Write your question here.
why don't have anyone to help me solve the problem
I think it's because your posts are in the form of a general statement. Suggestions:
1. Ask an actual question.
2. Be very specific about which part you need help with, what doesn't work, where you are stuck.
closed account (Nh79E3v7)
i need to do a c++ assignment to read a resistor colour code.....this is a question that i must to do

You are required to design a program to determine the value of a resistor based on resistor colour code. The program should prompt users to request the 4 bands colour marked on the resistor. The result should be display along with its tolerance. The user then asked whether he/she want to continue to determine the value of other resistor or not. If the user enter “Y” or “y”, the program will continue, otherwise enter “N”or “n” the program will stop. The program also should prompt an error message if the user enter an invalid colour code. Bonus marks will be given to any group that can show the correct error message in each selection of colour code.
That's still a statement. Which part do you need help with? Be specific.
closed account (Nh79E3v7)
this my code that i have done....but its not finish yet
#include <stdio.h>
#include <windows.h>
#include <math.h>
#include <string.h>

double colors(const char *colors[10],double);

int get_band (int n)
{ char input[10];
int i,condition;
const char *colors[10] = { "black", "brown", "red", "orange", "yellow",
"green", "blue", "violet", "grey", "white" };

printf ("Enter color of band# : " );
scanf ("%s", input);
for (int i=0; i<10; i++)
{
if (strcmp(input, colors[i]) == 0)
return i;
}
printf("press:\t1 to continue??\n\n \t2 to end??");
scanf("%d",&condition);

i=condition;


printf ("Invalid input\n");
return 0;
}

int main()
{ int band[3];
int value;
int i;

for (int i=0; i<3; i++)
band[i] = get_band(i);
value = band[0] * 10 + band[1];
value *= (double)pow(10.0, (double)band[2]);
printf ("The value of that resistor is %ld ohms\n", value);
system("pause");
return 0;
}

i need to plus value of tolerance and the user enter “Y” or “y”, the program will continue, otherwise enter “N”or “n” the program will stop. The program also should prompt an error message if the user enter an invalid colour code.
Well. it helps if you use code tags. [code]your code here[/code]
then it's possible to refer to particular line numbers, and show the indentation .. and so on.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

int get_band (int n)
{ 
    char input[10];

    const char *colors[10] = { "black", "brown", "red", "orange", "yellow",
    "green", "blue", "violet", "grey", "white" };

    printf ("Enter color of band %d : ", n );
    scanf ("%s", input);
    for (int i=0; i<10; i++)
    {
        if (strcmp(input, colors[i]) == 0)
            return i;
    }

    return 0;
}

int main()
{ 
    int band[3];
    int value;

    for (int i=0; i<3; i++)
        band[i] = get_band(i);
    
    value = band[0] * 10 + band[1];
    value *= (double)pow(10.0, (double)band[2]);
    printf ("The value of that resistor is %d ohms\n", value);
    
    system("pause");
    return 0;
}

Here I've tidied up the code a bit, and simplified it.

The program also should prompt an error message if the user enter an invalid colour code.
You might do do that by adding a while loop, so that lines 13 to 19 are repeated endlessly until a valid input is given. Print out the error message just after line 19.

i need to plus value of tolerance

Since the colours for the tolerance band are different, you might want to add another function to get that input from the user.

the user enter “Y” or “y”, the program will continue, otherwise enter “N”or “n” the program will stop

Perhaps add a while or do-while loop inside main(), so that lines 29 to 34 are repeated. Just after line 34, prompt the user to enter 'y' or 'n' and make the loop condition test that value.


One more thing - all of this code is C, not C++. That might be another reason why you get few helpful responses. Most folks here use C++ e.g. cin / cout instead of scanf / printf etc.
Last edited on
why don't have anyone to help me solve the problem

You received plenty of help in your previous thread, including a working program.
http://www.cplusplus.com/forum/beginner/191749/#msg924917

You should understand this is not a homework site. We won't do your homework for you.
We are happy to answer specific questions you have.

If you don't understand the code that I previously posted or that chervil reposted, then you need to be specific about what you don't understand. The program provided is quite simple. If you have no grasp of the concepts in the program (loops, arrays), then you need to go back and study the basics of the language.

I will also second what chervil mentioned about the assignment requesting a C++ program. What you posted is a C program, not C++. Your original posting made no mention of this being a C++ assignment.

i need to plus value of tolerance

My intent was that you understand how get_band() works for the value bands and create a similar function for the tolerance band. I'm not going to do all your work for you.

You have been asked multiple times to use code tags. PLEASE DO SO.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
If you're not going to make the slightest bit of effort to make your posts readable, why should we spend the slightest bit of effort helping you?
I will not respond further until you apply code tags.
Last edited on
Topic archived. No new replies allowed.