if else problems

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
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


int main()
{



	char choice;
	char a;
	char b;
	char m;
	char d;
	char r;
	char q;
	int n1,n2,answer,useranswer;
	bool quitnow = false;

	srand(static_cast<unsigned>(time(NULL)));
	n1 = rand()%20+1;
	n2 = rand()%20+1;
	answer = n1 = n2;



	cout << "a.Find the sum of two numbers" << a << endl;
	cout << "b.Subtract two numbers" << b << endl; 
	cout << "m.find the product of two numbers" << m << endl;
	cout << "d.Divide two numbers" << d << endl;
	cout << "r. find the Modulus of two numbers" << r << endl;
	cout << "q.Quit the program" << q << endl;
	cin >> choice;
	
	
	if(choice == a)
	{
		cout << "what is" << n1 << "+" << n2 << "?";
		cin >> useranswer;
	}
	else(choice == b);
	{
		cout << "what is" << n1 << "-" << n2 << "?";
		cin >> useranswer;
	}
	else(choice == m)
	{
		cout << "what is" << n1 << "*" << n2 << "?";
		cin >> useranswer;
	}
	else(choice == d)
	{
		cout << "what is" << n1 << "/" << n2 << "?";
		cin >> useranswer;
	}
	else(choice == r)
	{
		cout << "what is" << n1 << "%" << n2 << "?";
		cin >> useranswer;
	}
	else(choice == q)
	{
		quitnow = true;
	}
	return 0;
}


what is wrong with my program?
It should go something like this:
1
2
3
4
5
6
7
8
9
10
11
    if (choice == 'a')
    {
          // do something
        
    }
    else if (choice == 'b')
    {
         // do something 
    }
    
    // etc. 
First, you wouldn't need all the characters you declared.
You probably wanted to use "+" where you wrote
1
2
 answer=n1=n2;
//it should be answer=n1+n2; 

Then all your cout-s are all faulty.
All the variables declared are not initialized, yet you wanna display their content(which would be garbage). In your if statements, all other else would be else if but with the exception of the last else. Such that you would have
1
2
3
4
5
6
7
if (choice=='y')
//your code
else if(choice=='h')
//do this
//continue the else if until the last else which would be
else
//blah blah blah 
Hey guys I'm new. I tried looking all over but I can't grasp the idea. I downloaded C4droid on my nexus phone and am trying to make conditional responses. Here's what I have so far:

#include <iostream>
#include <string>
#include <sstream>

#define a "What is your name?"
#define b "How are you today?"

int main ( )
{
string str;
cout << "How may I assist you? forward slash n";
getline ( cin, str );
if ( str == a )
{
cout << " My name is Phi. forward slash n";
}
else ( str == b )
{
cout << " I am pleasant as always. forward slash n"
}
return 0;
}

now the problem is that whenever I type in "what is your name?" I get both responses: "My name is Phi. I am pleasant as always."

I want it to only say its name when I ask for its name. What am I doing wrong?

I apologize for not copy and pasting, I'm writing this on my laptop.

Okay nvm, I solved the issue... but now, how do I make it so that the program isn't so picky? Instead of having to put "How are you today?" I would like to put "how have you today" and still get the same result, or do I just have to make it so that when I define it, that's part of the definition?
Last edited on
@ tmalcuit90 This would be better as a new thread, but I'll try to answer here
This line has three problems:
else ( cin == b );

First, it should read else if , not just else.
Secondly, the line ends with a semicolon, which finishes that if-else structure. That means the following lines are always executed.
Third, the stream cin cannot be compared with a string like that.

In C++, it is preferred to use a const declaration instead of #define.
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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

const string a = "What is your name?";
const string b = "How are you today?";

int main ( )
{
    string str;
    cout << "How may I assist you? \n";
    getline ( cin, str );

    if ( str == a )
    {
        cout << " My name is Phi. \n";
    }
    else if ( str == b )
    {
        cout << " I am pleasant as always. \n";
    }
    return 0;
}
Topic archived. No new replies allowed.