What is wrong with this program

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main()
{
string x;
cout << "What's your husbands name? ";
cin >> William;
cout << "Did you know " << mystr << " loves you?\n";
else
cout << "Your not William's wife. \n";
system("PAUSE");
return EXIT_SUCCESS;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string x; // You're not using a declared variable.
    cout << "What's your husbands name? ";
    cin >> William; // You're reading input into an undeclared variable.
    cout << "Did you know " << mystr << " loves you?\n"; // You're trying to output an undeclared variable.
    else // Else without If
        cout << "Your not William's wife. \n";
    system("PAUSE"); // This whole line is poor style. But we can talk about that later.
    return EXIT_SUCCESS;
}

Last edited on
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 <cstdlib>
#include <iostream>
#include <string>
#include <windows.h> // Add this for later use of Sleep(milliseconds).
#define NEWLINE '\n' // I like adding this so that adding new lines are a bit easier to identify later on.

using namespace std;

int main()
{
  string name; // Change x to name, for simplicity.
    cout << "What's your husbands name? ";
    cin >> name; // Change William to name, as it is "string name;".
cout << NEWLINE; // For creating that new line.

	if (name == "William") // Means, IF user inputs "William", do the following.
      cout << "Did you know that " << name << " loves you?"; // Change mystr to name, as it is "string name;".

	else // Means, if user inputs something ELSE instead of "William", do the following.
      cout << "You're not William's wife.";
cout << NEWLINE; // Another new line.

  Sleep(2000); // Pauses for 2 seconds or 2,000 milliseconds. Try to stay away from 'system()' functions.

return EXIT_SUCCESS;
}


I've edited your code and added explanations. I hope this helps and if you need any further explanation on why I changed something, just ask. =)
where did you learned from? this page is a good reading for you...

http://cplusplus.com/doc/tutorial
Topic archived. No new replies allowed.