cin >>

I have 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
#include <iostream>

using namespace std;

int main () 
{
	char answer = 'a';
	char age = 'a';
	char name = 'a';
	float height = '0';

	cout << "Welcome to Crime Buster v 1.00\n\n";

	cout << "What would you like to do: \n\n";
	cout << "a: Enter New Suspect\n";
	cout << "b: View Current Suspects\n";
	cout << "c: Complete Crime Details\n";
	cout << "d: Add Additional Notes\n";
	cout << "e: Generate Conclusion\n";

	cout << "\n\n";
	cout << "Enter the letter of your desired action : ";
	cin >> answer;

	if (answer == 'a' || answer == 'b' || answer == 'c' || answer == 'd' || answer == 'e')
	{
		if (answer == 'a')
		{
			cout << "\n\nAdd New Suspect";
			cout << "\n\nName: ";
			cin >> name;
			cout << "\nAge: ";
			cin >> age;
			cout << "\nHeight: ";
			cin >> height;
			cout << "\n";
		};
	}

	return 0;
}	


but if you try to run it then when you select a and type in a name you can't enter anyhting else
Notice that 'name' and 'age' are of type char so they will get only one character.
Change them to string
You mean 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
#include <iostream>

using namespace std;

int main () 
{
	char answer = 'a';
	string age = 'a';
	string name = 'a';
	float height = '0';

	cout << "Welcome to Crime Buster v 1.00\n\n";

	cout << "What would you like to do: \n\n";
	cout << "a: Enter New Suspect\n";
	cout << "b: View Current Suspects\n";
	cout << "c: Complete Crime Details\n";
	cout << "d: Add Additional Notes\n";
	cout << "e: Generate Conclusion\n";

	cout << "\n\n";
	cout << "Enter the letter of your desired action : ";
	cin >> answer;

	if (answer == 'a' || answer == 'b' || answer == 'c' || answer == 'd' || answer == 'e')
	{
		if (answer == 'a')
		{
			cout << "\n\nAdd New Suspect";
			cout << "\n\nName: ";
			cin >> name;
			cout << "\nAge: ";
			cin >> age;
			cout << "\nHeight: ";
			cin >> height;
			cout << "\n";
		};
	}

	return 0;
}	


I'm not very experienced with strings what do I do?
Last edited on
Read for string info:

http://www.cplusplus.com/reference/string/string/

Then, in order to get information into strings, read this:

http://www.cplusplus.com/forum/articles/6046/

Also, to use std::string you need to #include <string>
Exactly...

Now the only thing you have to do is include Strings library.

#include <string>

http://cplusplus.com/reference/string/
More problems but I have got part of it working

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

#include <iostream>
#include <string> 

using namespace std;

int main () 
{
	char answer = 'a';
	string a;
	string b;
	string c;
		
	cout << "Welcome to Crime Buster v 1.00\n\n";

	cout << "What would you like to do: \n\n";
	cout << "a: Enter New Suspect\n";
	cout << "b: View Current Suspects\n";
	cout << "c: Complete Crime Details\n";
	cout << "d: Add Additional Notes\n";
	cout << "e: Generate Conclusion\n";

	cout << "\n\n";
	cout << "Enter the letter of your desired action : ";
	cin >> answer;

	if (answer == 'a' || answer == 'b' || answer == 'c' || answer == 'd' || answer == 'e')
	{
		if (answer == 'a')
		{
			cout << "\n\nAdd New Suspect\n";
			cout << "\n\nName: ";
			cin >> a;

			cout << "\nAge: ";
			cin >> b;

			cout << "\nHeight (Inches): ";
			cin >> c;

			
			
		}
	}

	return 0;
}	


but when I run it it just skips out Age:
Last edited on
You should use getline() when working with strings.

http://www.cplusplus.com/reference/string/getline/
You should use getline as Zhuge said. However, this code works. I replaced the if statement with a switch statement and it works as I think you want it to:
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
#include <iostream>

using namespace std;

int main () 
{
	char  answer = 'a'; // This can stay as a char.
	int      age = 0  ; // You could change this to char if you wanted though, like this: char age[3] <-- a 3 character char array.
	string  name = "" ; // This should be a string
	float height = 0.0;

	cout << "Welcome to Crime Buster v 1.00\n\n";

	cout << "What would you like to do: \n\n";
	cout << "a: Enter New Suspect\n";
	cout << "b: View Current Suspects\n";
	cout << "c: Complete Crime Details\n";
	cout << "d: Add Additional Notes\n";
	cout << "e: Generate Conclusion\n";

	cout << "\n\n";
	cout << "Enter the letter of your desired action : ";
	cin >> answer;

switch (answer)
{
    case 'a': // If the answer they inputted is a. You could use an if statement here, if you wanted to.
   		cout << "\n\nAdd New Suspect";
		cout << "\n\nName: ";
		cin >> name;
		cout << "\nAge: ";
		cin >> age;
		cout << "\nHeight: ";
		cin >> height;
		cout << "\n";   
}

	return 0;
}
Last edited on
When I try get line it just skips out age again when I try the exact code of chrisname it skips out age and height.
Try to add
system("pause");
before
return 0;


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

using namespace std;

int main () 
{
	char answer = 'a';
	string age;
	string name;
	float height = '0';

	cout << "Welcome to Crime Buster v 1.00\n\n";

	cout << "What would you like to do: \n\n";
	cout << "a: Enter New Suspect\n";
	cout << "b: View Current Suspects\n";
	cout << "c: Complete Crime Details\n";
	cout << "d: Add Additional Notes\n";
	cout << "e: Generate Conclusion\n";

	cout << "\n\n";
	cout << "Enter the letter of your desired action : ";
	cin >> answer;

	if (answer == 'a' || answer == 'b' || answer == 'c' || answer == 'd' || answer == 'e')
	{
		if (answer == 'a')
		{
			cout << "\n\nAdd New Suspect";
			cout << "\n\nName: ";
			cin >> name;
			cout << "\nAge: ";
			cin >> age;
			cout << "\nHeight: ";
			cin >> height;
			cout << "\n";
		}
	}

	system("pause");
	return 0;
}	
Last edited on
Try to add system("pause");

Right idea. Wrong code.

Use cin.get();
> Try to add system("pause")

What!?

http://www.cplusplus.com/forum/articles/11153/

[edit]
> Right idea. Wrong code.

/me Double takes.

What!? What?!!!!
[/edit]


ZHuge already gave you the correct answer. Don't cin >> mystring;. Instead #include <string> and use getline().

(The problem is that you are entering a name with spaces in it, like "Johnny Appleseed". Try again with a one-word name.) See also http://www.cplusplus.com/forum/articles/6046/


I actually just responded to another thread ( http://www.cplusplus.com/forum/beginner/11402/ ) with a handy little function to help with input. Try it here:
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <cctype>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
using namespace std;

//----------------------------------------------------------------------------
struct suspect_t
{
	string   name;
	unsigned age;
	unsigned height;  // in inches
};

//----------------------------------------------------------------------------
template <typename CharType, typename CharTraits>
std::basic_istream <CharType, CharTraits> &
endl( std::basic_istream <CharType, CharTraits> & ins )
{
	return ins.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
}

//----------------------------------------------------------------------------
char menu()
{
	char selection = '-';

	cout << "What would you like to do: \n\n";

	cout << "  a: Enter New Suspect\n";
	cout << "  b: View Current Suspects\n";
	cout << "  c: Complete Crime Details\n";
	cout << "  d: Add Additional Notes\n";
	cout << "  e: Generate Conclusion\n";
	cout << "  q: Quit\n\n";

	cout << "Enter your choice: " << flush;

	while (true)
	{
		cin >> selection >> endl;
		selection = tolower( selection );

		if (string( "abcdefq" ).find( selection ) != string::npos) break;

		cout << "Try again: " << flush;
	}

	return selection;
}

//----------------------------------------------------------------------------
suspect_t add_new_suspect()
{
	suspect_t result;

	cout << "\n\nAdd New Suspect\n";

	while (result.name.empty())
	{
		cout << "\nName: " << flush;
		getline( cin, result.name );
		// (you may want to add some processing here, like to get
		//  rid of any leading and trailing spaces in the name, etc)
	}

	while (true)
	{
		cout << "\nAge: " << flush;
		cin >> result.age >> endl;
		if (cin) break;
		cin.clear();	// So if the user entered anything invalid, fix cin
		cin >> endl;	// and get rid of the offending input
	}

	while (true)
	{
		cout << "\nHeight (Inches): " << flush;
		cin >> result.height >> endl;
		if (cin) break;
		cin.clear();
		cin >> endl;
	}

	return result;
}

//----------------------------------------------------------------------------
int main () 
{
	bool done = false;
	vector <suspect_t> suspects;

	cout << "Welcome to Crime Buster v 1.00\n\n";

	while (!done)
	switch (menu())
	{
		case 'a':
			suspects.push_back( add_new_suspect() );
			break;

		case 'q':
			done = true;
	}

	return 0;
}	

You'll notice that I added functions and a way to remember suspects (via a vector and a class).
I added a switch statement to process menu selections.
I also added a way to quit -- don't forget that.
There is also some rudementary error handling -- preventing the user from inputting nonsense.

Add the stuff for selection 'b' (View Current Suspects) to see how it worked.

Hope this helps.
Last edited on
your trying to fit a string into a pre intialized char. you need to change the char to a string and declare it as string name = ""; make sure to #include <string>
Topic archived. No new replies allowed.