Can't clear memory used.

This is a discussion on Can't clear memory used. within the C++ Programming forums, part of the General Programming Boards category; Hi all, The program can't clear the memory space after user entered a invalid choice. It works fine for numeric ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Can't clear memory used.

    Hi all,

    The program can't clear the memory space after user entered a invalid choice. It works fine for numeric 0 - 9 but if i enter an alphabet, the text 'invalid choice' will be displayed twice (IE: after showing the error message, it'll show again in "Enter Choice: ", hence preventing user from entering a choice).

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    // To pause the screen for user to read.
    void pause(void)
    {
    	getchar();
    }
    
    void main(void)
    {
    
    	int choice;
    
    	do
    	{
    		system("cls");
    
    		cout << "=========================" << endl;
    		cout << "1.) Contacts" << endl;
    		cout << "2.) Messaging" << endl;
    		cout << "3.) Make Call" << endl;
    		cout << "4.) Call Log" << endl;
    		cout << "5.) Accessories" << endl;
    		cout << "6.) Tools" << endl;
    		cout << "7.) Off Mobile" << endl;
    		cout << "=========================" << endl;
    		
    		cout << "Please select a menu: ";
    		cin >> choice;
    
    		switch (choice)
    		{
    			case 1 : contacts();
    				break;
    			case 2 : cout << "go to messaging" << endl;
    				pause();
    				break;
    			case 3 : cout << "go to make call" << endl;
    				pause();
    				break;
    			case 4 : cout << "go to call log" << endl;
    				pause();
    				break;
    			case 5 : accessories();
    				break;
    			case 6 : tools();
    				break;
    			case 7 : // prints quit msg.
    				break;
    
    			default:
    				cout << "Invalid Choice!" << endl;
    				pause();
    				break;
    		}
    		
    	} while (choice!=7);
    	  cout << "Program terminating..." << endl;
    }
    I can't find where the error lies... hope someone can help me out.

    PS: There are other functions in this program which I didn't paste it out because it's not revelant to the error that's occuring.

    Thanks

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Get rid of void main before a mod catches you using it.

    The program goes into an error state if a char is entered into an int variable.

    cin.clear(1);
    cin.ignore(80, '\n');

    Since you're using digits for you choices, just use a char variable and

    switch(char variable)
    {
    case '1': ....

    ..//

    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    Get rid of void main before a mod catches you using it.
    Eh, hehe, i don't really understand what you mean by this? You mean void main should not appear in any codes i pasted?

    Anyway, i'm not sure about the cin.clear(1);

    Hmm, i'll give it a try and let you know.

    Thanks

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Heh... it's late and I'm sleepy. I don't use this namespace stuff normally, but I decided to play with it here. For you purists, if it's wrong, then it's because I don't use it

    Try this. Hopefully it will work.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
      int try1; 
        
      static char MENU[] = "=========================\n"
    		       "1.) Contacts\n"
    		       "2.) Messaging\n"
    		       "3.) Make Call\n"
    		       "4.) Call Log\n"
    		       "5.) Accessories\n"
    		       "6.) Tools\n"
    		       "7.) Off Mobile\n"
                           "========================\n";
                                     
      do
      {
         std::cout << endl << MENU;
         std::cin >> try1;
         
         if(std::cin.fail()) // int variable try an alpha char
         {
         	std::cin.clear();
         	std::cin.ignore(80, '\n'); // clean up the junk
         }
         
         switch(try1)
         {
            case 1  : std::cout << "Contacts" << endl;
                      break;
            
            case 3  : std::cout << "Make Call" << endl;
                      break;
            case 999: break;
            default : std::cout << try1 << " is invalid.";
                      break;
         }
      }while(try1 != 999);             
     
    	
      return EXIT_SUCCESS;
    }
    void main() is not a good thing to do. Search the board for why not.
    Last edited by ronin; 03-12-2003 at 11:45 PM.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  5. #5
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    once you are "using namespace std" you don't have to put it at the start of each cout etc, it's redundant - and afaik u don't need the (void) after main either - just make it "int main()"
    but otherwise looks good
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    I'm not sure about namespace since I never came across it or use it before.

    ronin I solved it using cin.ignore where I change the variable of type int to char and added the cin.ignore line just after pause(); in default case.

    Anyway, thanks for all the help guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 04:22 AM
  5. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 07:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21