Thread: Trouble with code, cin not working

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    Lightbulb Trouble with code, cin not working

    Compile this. Then run it. Hit 1,"enter a task."Then try to hit 1 again next time. IT won't recive input. Des anyone know why?
    I apoligize in advance for being a newb.
    Code:
    #include <string>
    #include <iostream>
    #include <vector>
    int tasks[40];
    int num =0;
    int numm =0;
    int i=0;
    using namespace std;
    int input =0;
    void taskmanager();
    	  
    
    void main (){ 
    taskmanager();
      
    	
      cout << "\nBye\n\n\n\n\n\nAll Code Copyright Patrick \n\n\nAll Rights Reserved\n";
     }
    
    void taskmanager()
    {
    while(num==numm)
    {if(input ==0)
    
    {
    	input= 7;
    	std::cout << "Hello \n This is your friendly task manager \n Select a command \n";
     std::cout << "1) Add a task \n2) Read next on list\n3)Quit\n";
     std::cin >> input;}
      
      if ( input == 1)
      {cout<< "\nOK, enter your task:\n ";
      cin >> tasks[i];
      i++;
      input = 0;
      }
      if (input == 2)
    	  if (i==0)
    	  {cout<<"\nSorry, no items to read\n";
    	  input = 0;
    	  }
    if (input == 2)
      { cout << "\nDO THIS:\n";
      cout << tasks[(i-1)];
    input = 0; }
    if (input == 3)
    {return;
    }
    }
    };

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    i will check your program for you but first of all EHHH dont void main! main must return an integer. as my compiler clearly repeats to me...

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    also i dont understand, why include string if your asking for an integer for each task? do you want to input a string instead?

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    here is an example of what you were currently trying to do, but with correct syntax, make sure you dont put ";" after void tastmanager(){}; < when your not defining it you dont need to put it for implementation. also when you declare using namespace std, the compiler assumes the std:: prefix for everything so you dont need to put it.

    Code:
    #include <string>
    #include <iostream>
    #include <vector>
    using namespace std;
    void taskmanager();
    	  
    
    main ()
    { 
    taskmanager();
    cout << "\nBye\n\n\n\n\n\nAll Code Copyright Patrick \n\n\nAll Rights Reserved\n";
    
    }
    
    void taskmanager()
    {
       int tasks[40];
       int input, num, i = 0;
       bool exit = false;   
       while (!exit)
       {
          cout << "\nHello \n This is your friendly task manager \n Select a command \n";
          cout << "1) Add a task \n2) Read next on list\n3)Quit\n";   
    
          cin >> num;
          switch (num)
          {
             case 1:
                cout<< "\nOK, enter your task:\n ";         
                cin >> tasks[i];
                i++;  
                break;
             case 2:
                if (i==0)
       	        {
                   cout<<"\nSorry, no items to read\n";
    	           input = 0;
      	        }
                else
                { 
                   cout << "\nDO THIS:\n";
                   cout << tasks[(i-1)];
                   input = 0; 
                }
                break;
             case 3:
                exit = true;
                break;
             default: 
                cout << "Invalid Input";
          }
       }
    }

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    finished:
    see if you can understand this let me know if you cant i can explain.
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    void taskmanager();
    
    main ()
    { 
       taskmanager();
       cout << "\nBye\n\n\n\n\n\nAll Code Copyright Patrick \n\n\nAll Rights Reserved\n";
       return 0;
    }
    
    void taskmanager()
    {
       string task[40];        //for spaces to be allowed, must be a string
       int input, num, i = 0;
       bool exit = false;      //becomes true when exit is selected
       while (!exit)
       {
          cout << "\nHello \n This is your friendly task manager \n Select a command \n";
          cout << "1) Add a task \n2) Read next on list\n3) Quit\n";   
    
          cin >> num;     //enter selection
          switch (num)    //decides what to do for the number entered
          {
             case 1:
                cout<< "\nOK, enter your task:\n ";
                cin.get();                //must dump enter before filling string
                getline( cin, task[i] );  //fills string using cin (keyboard)            
                i++;  
                break;
             case 2:
                if (i==0)
       	        {
                   cout<<"\nSorry, no items to read\n";
    	           input = 0;
      	        }
                //else is needed because if there is nothing there
                //you dont want this to execute
               else           
                { 
                   cout << "\nDO THIS:\n";
                   cout << task[(i-1)];
                   input = 0; 
                }
                break;
             case 3:
                exit = true;   //if 3 is chosen than exit is true            
                break;
             default: 
                cout << "Invalid Input"; //what happens if they enter 5? this does...      
          }
       }
    }
    Last edited by JoshR; 04-12-2005 at 09:28 PM.

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Code:
    #include <string>
    #include <iostream>
    #include <vector>
    using namespace std;
    
    string tasks[40]; //string task would be better than int task
    // int num = 0, numm = 0; not really used at all
    int i = 0;
    int input =0;
    void taskmanager();
    
    int main () // mian returns an integer
    { 
    	taskmanager();
    	cout << "\nBye\n\n\n\n\n\nAll Code Copyright Patrick \n\n\nAll Rights Reserved\n";
    }
    
    void taskmanager()
    {
    	while(true) // since you never changed num or numm, might as well cut to the chase
    	{
    		if(input ==0 )
    		{
    			input= 7;
    			std::cout << "Hello \n This is your friendly task manager \n Select a command \n";
    			std::cout << "1) Add a task \n2) Read next on list\n3)Quit\n";
    			if (!cin)
    			{
    				cin.clear();
    				cin.ignore(100);
    			}
    			std::cin >> input;
    		}
    
    		if ( input == 1)
    		{
    			cout<< "\nOK, enter your task:\n ";
    			cin.ignore(1);
    			getline(cin, tasks[i], '\n') ;
    			
    			
    			i++;
    			input = 0;
    		}
    		if (input == 2)
    		{
    			if (i==0)
    			{
    				cout<<"\nSorry, no items to read\n";
    				input = 0;
    			}
    			else
    			{
    				//if (input == 2) we alread know input ==2 or we wouldn't be here
    				cout << "\nDO THIS:\n";
    				cout << tasks[i-1] << endl;
    				input = 0;
    			}
    		}
    
    		if (input == 3)
    		{
    			return;
    		}
    	}
    };
    This is close to your original, I change task to a string and also I used getline since we may want to enter a whole sentence and not just a single word

    The program could still use some error checking

  7. #7
    *this
    Join Date
    Mar 2005
    Posts
    498
    i think what i posted is less prone to errors, although a switch is involved, i dont know if you are there yet. but the last thing to do to make it even less prone to errors is to have the input be of type char, and have the switch test for cases '1' - '3', that way if a character is entered than the default is executed.

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    btw a void function cannot return a value.

  9. #9
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by JoshR
    btw a void function cannot return a value.
    he doesn't return a value from a void function.

  10. #10
    *this
    Join Date
    Mar 2005
    Posts
    498
    im just stating it, because he obviously doesnt get when void should be used in declerations.

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    2
    Thank you guys, for both the tips and for fixing the code. You rock.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2009, 08:37 PM
  2. Help Code Is Not Working
    By srivatsan in forum C++ Programming
    Replies: 23
    Last Post: 11-12-2008, 12:30 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM