Program not running? Fatal Error stated on Visual STudio 2005

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

Here is my code:
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Resistor Calculation.cpp : main project file.

#include <iostream>
using namespace 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;
			}
			

		}
		else if (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;
}
I'm using Visual Studio 2005
You should get a newer version http://www.microsoft.com/exPress/download/

"fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?"
Start from an Empty Project
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?
also how would i set this program up using arrays?
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).

You need to modify your calculation loop.
yes trying to treat the value of 0 as a short circuit!

and how would i modify the loop?

Just like this should do it:
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
  else if (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;

    }
hmm...still getting 75 !?
I'm not - but let me recheck

Well I've checked and here is the code used:
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
using namespace 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;
      }


    }

    else if (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.
Last edited on
kk thanks.
thank you again! guestgulkan.

also how would you implement this code into arrays....not a fan of arrays but would still like to know.
Trying to set it up like this:

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]; 

}
 
On late shift this week - maybe a day or two before I can give a sensible answer.
or would it be something like this?

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
39
40
41
42
43
44
45
46
47
48
49

    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 = new double[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;
} 

Topic archived. No new replies allowed.