Can not input values into variables.

the basis of my problem is that i am new to c++ and cant debug this myself.
everything runs fine, the only thing is i can not input values into variables so that the can be calculated. any solutions welcome and if one is given can you please tell me what i did so don't repeat the mistake. thank you
i am using microsoft visual 2012.

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//power and ohms law calculator, statement input is to decide wanted outputs with obtained numerical inputs
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
void main()
{
	char Statement[25]; //space for character input
	double V, P, I, R, Amps, Volts, Watts, Ohms;

	cout<<"Enter two of the following with 'and' between them\nvoltage, power, resistance or current."<<endl;
	cin>>Statement;

	if (_stricmp("voltage and power", Statement)||("power and voltage", Statement)==0)
				//_stricmp is string compare, if Statement is voltage and power or other way round.
	{
	cout<<"Enter Current"<<endl;
	cin>>I;
	cout<<"Enter Resistance"<<endl;
	cin>>R;
	Watts=(I*I)*R;
	Volts=I*R;
	cout<<"The Results are, "<<Volts<<" V\n"<<endl;
	cout<<Watts<<" W"<<endl;
	}

	else if (_stricmp("current and power",Statement)||("power and current", Statement)==0)
						//_stricmp is string compare, if  Statement is current and power or other way round.
	{
	cout<<"Enter Voltage"<<endl;
	cin>>V;
	cout<<"Enter Resistance."<<endl;
	cin>>R;
	Watts=(V*V)*R;
	Amps=V/R;
	cout<<"The Results are, "<<Amps<<" A\n"<<endl;
	cout<<Watts<<" W"<<endl;
	}

	else if (_stricmp("resistance and power", Statement)||("power and resistance", Statement)==0)
						//_stricmp is string compare, if  Statement is resistance and power or other way round.
	{
	cout<<"Enter Current."<<endl;
	cin>>I;
	cout<<"Enter Voltage."<<endl;
	cin>>V;
	Watts=V*I;
	Ohms=V/I;
	cout<<"The Results are, "<<Ohms<<" Ohms\n"<<endl;
	cout<<Watts<<" W"<<endl;
	}

	else if (_stricmp("voltage and current", Statement)||("current and voltage", Statement)==0)
						//_stricmp is string compare, if  Statement is voltage and current or other way round.
	{
	cout<<"Enter Power."<<endl;
	cin>>P;
	cout<<"Enter Resistance."<<endl;
	cin>>R;
	Amps=sqrt(P/R);
	Volts=sqrt(P*R);
	cout<<"The Results are, "<<Volts<<" V\n"<<endl;
	cout<<Amps<<" A"<<endl;
	}

	else if (_stricmp("voltage and resistance", Statement)||("resistance and voltage", Statement)==0)
						//_stricmp is string compare, if  Statement is voltage and resistance or other way round.
	{
	cout<<"Enter Current."<<endl;
	cin>>I;
	cout<<"Enter Power."<<endl;
	cin>>P;
	Ohms=P/(I*I);
	Volts=P/I;
	cout<<"The Results are, "<<Volts<<" V\n"<<endl;
	cout<<Ohms<<" Ohms"<<endl;
	}

	else if (_stricmp("resistance and current", Statement)||("current and resistance", Statement)==0)
						//_stricmp is string compare, if Statement is voltage and current or other way round.
	{
	cout<<"Enter Power."<<endl;
	cin>>P;
	cout<<"Enter Voltage."<<endl;
	cin>>V;
	Ohms=(V*V)/P;
	Amps=P/V;
	cout<<"The Results are, "<<Ohms<<" Ohms\n"<<endl;
	cout<<Amps<<" A"<<endl;
	}
}


Output
Enter two of the following with 'and' between them
voltage, power, resistance or current.
voltage and power
Enter Current
Enter Resistance
The Results are, 8.56729e+123 V

-7.92985e+185 W
Press any key to continue . . .
I'm not sure what is wrong with your code, but if you make a typo while entering "Statement" it will somehow allow you to enter values for some variables.
I've found two problems. First, Statement does not take all of the input you give it. It only takes up until the first space. Run your code with this and you'll see what I mean:

1
2
3
cout<<"Enter two of the following with 'and' between them\nvoltage, power, resistance or current."<<endl;
cin>>Statement;
cout << "Statement: " << Statement;


Second, all of your if statements need to be in this form:

if ((_stricmp("voltage and power", Statement) == 0) || (_stricmp("power and voltage", Statement) == 0))
Last edited on
I'm fairly new to C++ myself, so I don't really know what the solution to this problem is, but I found the following workaround. Make all of your if statements like this:

if ((_stricmp("voltage_and_power", Statement) == 0) || (_stricmp("power_and_voltage", Statement) == 0))

and type in your input with underscores instead of spaces. For example:

Enter two of the following with 'and' between them
voltage, power, resistance or current.
voltage_and_power
Enter Current
0.050
Enter Resistance
1000
The Results are, 50 V

2.5 W
Press any key to continue . . .
Okay, I finally figured it out. You have to use "cin.getline()". If you use cin << it will ignore everything after the first space.

1
2
cout<<"Enter two of the following with 'and' between them\nvoltage, power, resistance or current."<<endl;
cin.getline(Statement, 30);
you can also use getline(cin, stringVariable)
make sure u convert the input string using the tolower() function such that even if the user inputs upper cases, it won't generate error in comparing.
first of all thank ou guys for the quick responces and for your time. i have got it working properly as far as i can see. thank you.

BridgesBenghazi
make sure u convert the input string using the tolower() function such that even if the user inputs upper cases, it won't generate error in comparing.
how would i implement that? also it seems to work with caps on with out tolower()

BrentSpinor
thank you for your input, i used "cin.getline(...)" and your correction to put both options of the if statement behind its own "_stricmp(....."

Aramil of Elixia
you can also use getline(cin, stringVariable)
how is that diferent to cin.getline(...)? and what are the pros/cons of it over c.getline(...)?

and on a side note, how easy would it be to edit this code so that i could loop it back to the start of the code again to input different values and how could i go about it?
thank you all, i think i am goin to love this forum :)
I wouldn't be difficult to loop it back to the start. Just put a while(true) loop around evenything in the main function. Then, change your cout statement to this:

cout<<"Enter two of the following with 'and' between them or enter 'quit' to exit\nvoltage, power, resistance or current."<<endl;

Then add one more else if statement just before the end of the loop:


1
2
else if (_stricmp("quit", Statement) == 0)
    break;
cheers, works a treat
Topic archived. No new replies allowed.