I'm using Visual Studio 2005, and after building the solution. An error comes up
stating "fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?"
What is wrong with my code I put <iostream>
along with the std
// Resistor Calculation.cpp : main project file.
#include <iostream>
usingnamespace std;
int main()
{
char type;
float total_resistance=0;
float temp=0;
int number_of_resistors_connected=0;
float resistance_found=0;
while (1){
cout<<"Please input 's' for series OR 'p' for resistors parallel"<<endl;
cin>>type;
cout<<"Please enter the number of registers connected"<<endl;
cin>>number_of_resistors_connected;
if (type=='s')
{
for (int i=0 ; i<number_of_resistors_connected; i++)
{
cout<<"Input serial value"<<i+1<<endl;
cin>>temp;
total_resistance=total_resistance+temp;
}
}
elseif (type=='p')
{
for (int i=0 ; i<number_of_resistors_connected; i++)
{
cout<<"Input parallel value"<<i+1<<endl;
cin>>temp;
if (temp!=0)
temp=1/temp;
total_resistance=total_resistance+temp;
}
if (total_resistance!=0)
total_resistance=1/total_resistance;
}
else
{
cout<<"Incorrect input letter, enter 's' OR 'p'"<<endl;
}
if (type=='s' || type=='p')
break;
}
if (total_resistance<1000)
{
int x=0;
x=total_resistance;
cout<<"The total resistance is less than 1000 and its value is "<<x<<endl;
}
else
{
total_resistance=total_resistance/1000;
cout<<"The total resistance is greater than 1000 and its value is "<<total_resistance<<endl;
}
return 0;
}
thank bazzy for the heads up...went to my program properties and said not to use precompiled headers and it ran.
also i added a set precision to my code.
else
{
total_resistance=total_resistance/1000;
cout<<fixed<<showpoint<<setprecision(3);
cout<<"The total resistance is greater than 1000 and its value is "<<total_resistance<<endl;
}
*trying to follow this statement.
when pressing "p" for parallel and entering 3 resistor values: 120.5, 0, and 200
end value should be 0
but i keep on getting 74 or 75 forgot the number.
i think i have to add something to my code, not change it, to satisfy this! how do i do it?
do i use a flag?
It all depends on how you are treating an input of 0.
If you treat it as an error and ignore it (like you are doing at the moment) then
with 3 resistors of 120.5, 0, and 200 - you will get the result of 75 ohms.
If you treat a value of 0 as a short circuit - then the result should automatically be zero if you include a value of 0 - because no matter how many resistors you have in parallel with a short circuit - the result is a short circuit (0 ohms).
elseif (type=='p')
{
for (int i=0 ; i<number_of_resistors_connected; i++)
{
cout<<"Input parallel value"<<i+1<<endl;
cin>>temp;
if (temp!=0)
{
temp=1/temp;
total_resistance=total_resistance+temp;
}
else
{
//we have a short cct
total_resistance =0; //total resistance is zero
break;// quit the loop now
}
}
if (total_resistance!=0)
total_resistance=1/total_resistance;
}
#include <iostream>
usingnamespace std;
int main()
{
char type;
float total_resistance=0;
float temp=0;
int number_of_resistors_connected=0;
float resistance_found=0;
while (1){
cout<<"Please input 's' for series OR 'p' for resistors parallel"<<endl;
cin>>type;
cout<<"Please enter the number of registers connected"<<endl;
cin>>number_of_resistors_connected;
if (type=='s')
{
for (int i=0 ; i<number_of_resistors_connected; i++)
{
cout<<"Input serial value"<<i+1<<endl;
cin>>temp;
total_resistance=total_resistance+temp;
}
}
elseif (type=='p')
{
for (int i=0 ; i<number_of_resistors_connected; i++)
{
cout<<"Input parallel value"<<i+1<<endl;
cin>>temp;
if (temp!=0)
{
temp=1/temp;
total_resistance=total_resistance+temp;
}
else
{
//we have a short cct
total_resistance =0; //total resistance is zero
break;// quit the loop now
}
}
if (total_resistance!=0)
total_resistance=1/total_resistance;
}
else
{
cout<<"Incorrect input letter, enter 's' OR 'p'"<<endl;
}
if (type=='s' || type=='p')
break;
}
if (total_resistance<1000)
{
int x=0;
x=total_resistance;
cout<<"The total resistance is less than 1000 and its value is "<<x<<endl;
}
else
{
total_resistance=total_resistance/1000;
cout<<"The total resistance is greater than 1000 and its value is "<<total_resistance<<endl;
}
return 0;
}
Here is the result when I try to enter the three values 120.5, 0, 200.
As you can see it quits the loop as soon as I enter 0 - (so I don't get to enter the 200) and it
returns the value of 0.
Please input 's' for series OR 'p' for resistors parallel
p
Please enter the number of registers connected
3
Input parallel value1
120.5
Input parallel value2
0
The total resistance is less than 1000 and its value is 0
Maybe for the final printout when the total resistance is greater than 1000 and you divide it by 1000 - maybe you should put a K (to show kilo-ohms) after the value otherwise it looks a bit strange.
Print Output Format: Print total resistance followed by the values of each resistor.
For series: (Example)
Total series resistance is 2.123 kOhm for 3 resistors:
1000 kOhm, 1000 kOhm, 123 kOhm
For parallel: (Example)
Total series resistance is 500 Ohm for 2 resistors:
1000 Ohm, 1000 Ohm
would my code be like this (*taking the last if and else statements out and replacing it with two for loops? one for the parallel and the other for the series?)
this is for series
1 2 3 4 5 6 7 8 9 10 11 12
for(int i = 0; i < number_of_resistors_connected; i++)
{
int x=0;
x=total resistance;
cout<<"Total series resistance is"<<x<< kOhm
for<<total_resistance<<"resistors<<endl;
total_resistance += resistor_value[i];
}
double *r;
while (1) {
cout << "Please input 's' for series OR 'p' for resistors parallel"
<< endl;
cin >> type;
cout << "Please enter the number of resistors connected" << endl;
cin >> number_of_resistors_connected;
if (!cin || (number_of_resistors_connected <= 0)) {
cerr << "Invalid input. Must be a positive integer." << endl;
exit(EXIT_FAILURE);
}
r = newdouble[number_of_resistors_connected];
if (type == 's') {
total_resistance = 0.0;
for (int i = 0; i < number_of_resistors_connected; i++) {
cout << "Input value of series resistor number " << i + 1 << endl;
cin >> r[i];
if (!cin) {
cerr << "Invalid input." << endl;
exit(EXIT_FAILURE);
}
total_resistance += r[i];
}
}
.
.
.
// would i do the same for parallel resistors? just different formula?
.
.
.
} // End of the "while(1) loop
.
.
.
//
.
.
.
// how do i Make a loop that prints values of r[0], r[1], ...???
.
.
.
delete [] r;
return 0;
}