My program wont let me put in an input! any sugg?

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


float choice; //integer to hold a value to determine which fucntion the user wants by using an if statment


int
main()
{

float choice; //integer to hold a value to determine which fucntion the user wants by using an if statment
int string;

cout<<"Hello, please enter a string of text"<<endl;
cin>>string;
cout<<"You have 3 programs you may use\n";
cout<<"There is a letter counter, a letter sorter and a word counter\n";
cout<<"Please select which program you wish to use"<<endl;
cout<<"For letter counter type 1"<<endl;
cout<<"For letter sorter type 2"<<endl;
cout<<"For word counter type 3"<<endl;
cout<<"Please choose 1, 2 or 3"<<endl;
cin >> choice;


if(choice==1) {
cout<<"You have chosen char counting"<<endl;
}

else {
if(choice==2) {
cout<<"You have chosen char sorting;"<<endl;
}

else {
if(choice==3) {
cout<<"You have chosen char count;"<<endl;
}

//else { cout<<"Invalid selection please select 1,2 or 3"<<endl;}

}
}

return(0);

}
Last edited on
cin>>string; will read the int you previously declared, not a string

And please use [code][/code] tags
Last edited on
Treat strings like you would any other variable. string naem;

Also, are you sure it's a good idea have choice as a floating point integer? Your comparisons shall not work; the constant integers are missing decimal points.

-Albatross
this change will allow your program to recieve a string.

***EDITED***

1
2
3
4
5
6
7

string string;

cout<<"Hello, please enter a string of text"<<endl;
getline(cin, string);
cout<<"You have 3 programs you may use\n";
Last edited on
mcgeough ,if you have multiple choices more than two use switch statement it is much cleaner and it's an alternative to nested if ...else like this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch( num )
{
case 2: cout << "Case 2 executed!" << endl;
break;
case 4: cout << "Case 4 executed!" << endl;
break;
case 6: cout << "Case 6 executed!" << endl;
break;
case 8:cout << "Case 8 executed!" << endl;
break;
default:
cout << "Default case executed implies you do not ";
cout << "enter a 2, 4, 6, or 8." << endl;
break;
}
op put
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
#include<iostream>
#include<string>
using namespace std;


float choice; //integer to hold a value to determine which fucntion the user wants by using an if statment


int
main()
{

float choice; //integer to hold a value to determine which fucntion the user wants by using an if statment
int string;

cout<<"Hello, please enter a string of text"<<endl;
cin>>string;
cout<<"You have 3 programs you may use\n";
cout<<"There is a letter counter, a letter sorter and a word counter\n";
cout<<"Please select which program you wish to use"<<endl;
cout<<"For letter counter type 1"<<endl;
cout<<"For letter sorter type 2"<<endl;
cout<<"For word counter type 3"<<endl;
cout<<"Please choose 1, 2 or 3"<<endl;
cin >> choice;


if(choice==1) {
cout<<"You have chosen char counting"<<endl;
}

else {
if(choice==2) {
cout<<"You have chosen char sorting;"<<endl;
}

else {
if(choice==3) {
cout<<"You have chosen char count;"<<endl;
}

//else { cout<<"Invalid selection please select 1,2 or 3"<<endl;}

}
}

return(0);

}

ok first you want shorten up your code a little bit so you have less useless lines
ex:
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
#include<iostream>
#include<string>
using namespace std;
float choice; //integer to hold a value to determine which fucntion the user wants by using an if statment
int //delete this line
main() // change to 'int main()'
{
float choice; //integer to hold a value to determine which fucntion the user wants by using an if statment
int string;// change string to 's' its easyer to work with to get the exact code that you want and you can easyly change it back later
cout<<"Hello, please enter a string of text"<<endl;
cin>>string;// read the comment on line 10
cout<<"You have 3 programs you may use\n";// you can delete all the extra 'cout's' and it can be all one line see ex2
cout<<"There is a letter counter, a letter sorter and a word counter\n";
cout<<"Please select which program you wish to use"<<endl;
cout<<"For letter counter type 1"<<endl;
cout<<"For letter sorter type 2"<<endl;
cout<<"For word counter type 3"<<endl;
cout<<"Please choose 1, 2 or 3"<<endl;
cin >> choice;
if(choice==1) {
cout<<"You have chosen char counting"<<endl;
}
else {
if(choice==2) {
cout<<"You have chosen char sorting;"<<endl;
}
else {
if(choice==3) {
cout<<"You have chosen char count;"<<endl;
}
//else { cout<<"Invalid selection please select 1,2 or 3"<<endl;}
}
}
return(0);
}


ex2:
1
2
cout<<"You have 3 programs you may use\nThere is a letter counter, a letter sorter and a word counter\nPlease select which program you wish to use.\nFor letter counter type 1.\nFor letter sorter type 2.\nFor word counter type 3.\nPlease choose 1, 2 or 3"<<endl;
cin >> choice;


this should beable to help save some space and if you do put this in toyour code and it fails then on the lines that i provided change to '.....\n.......' > to > '........\n" << ".......\n"<< etc.'

if it still fails pm me and i will try to figure out what needs to be changed but also try what everyone else is saying to good luck and hapy coding
Am I the only one who noticed that he declared "float choice;" twice?
@ packetpirate: I have noticed this but isn't float choice allowed I'm still kinda new at this and I'm learning as I go along personally i would rewrite this whole code but I also gotta remember that most of these questions are homework questions and I'm just programing for fun.
Topic archived. No new replies allowed.