Am trying too much too soon

Hi, im currently attempting to learn c++ by myself, I have understood a little and wanted to try this program. The aim of this is to get the user to input a destination, then the airport (flying from) and then if the first input (aa) is greece AND the second input (bb) is birmingham the program will output one of the relevant IF statements.

I have deduced that if I enter more than one letter the program wont work, what am I missing, I am very receptive to criticism and will always appreciate all advice given.

Thank you in advance.

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
  #include <iostream>
  #include <cstring>

using namespace std;

int main()
{
    char aa;
    char bb;
    char greece;
    char birmingham;


    cout << "what destination are you travelling? \n";
    cin >> aa;
    cout << "where will you be flying from? \n";
    cin >> bb;


    if ((aa == greece) && (bb == birmingham)){
        cout << "ahh usual summer holiday? \n";
    }


    if  ((aa != greece) && (bb != birmingham)){
        cout << "where are you flying from then? \n";
    }
    return 0;
}
use std::string instead of char. Also you never put anything in your greece or Birmingham variables.
Thank you Yanson!!!

When you say never put anything in the greece or birmingham variables, what exactly do you mean? Leave a blank space or literally remove std::string birmingham etc? I do apologise but I am really enjoying learning this and I have only been doing it for 2 days in total.

Thanks guys.
I'm also a total beginner, but:

Further to what Yanson said: if you use std::string, you'll need to change #include <cstring> to #include <string>

When he says you didn't put anything for Greece or Birmingham, he means you declared a variable, but didn't give it any value (either at the moment of initializing or later). You then call that non-existent variable in a function, which won't work.

Or to put it another way, this is ok:

1
2
3
4
5
6
7
8
9
10
#include "stdafx.h"
#include <iostream>
#include <string>

int main()
{
	std::string hello;
	hello = "Hello";
	std::cout << hello << "\n";
}


But this is not ok (or at least, it's a program that does nothing):

1
2
3
4
5
6
7
8
9
#include "stdafx.h"
#include <iostream>
#include <string>

int main()
{
	std::string hello;
	std::cout << hello << "\n";
}


Here's your program re-written using std::string. As you can see, i've removed the greece and birmingham chars (or strings) in lines 10 & 11, they're not necessary!

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
	std::string aa;
	std::string bb;
	
	cout << "what destination are you travelling? \n";
	cin >> aa;
	cout << "where will you be flying from? \n";
	cin >> bb;


	if ((aa == "greece") && (bb == "birmingham")) {
		cout << "ahh usual summer holiday? \n";
	}


	if ((aa != "greece") && (bb != "birmingham")) {
		cout << "where are you flying from then? \n";
	}
	return 0;
}
Last edited on
Topic archived. No new replies allowed.