Thread: Q to quit help

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    39

    Q to quit help

    Hi

    Below is a script that im making. Its going to be a simple phone directory. You Enter A Name and it will give you that persons phone number..

    Neways..I was wondering how you would impliment a Press Q To Quit in there. Ive tried the if(name == 'q') but it doesnt compile nicely

    Cheers For The Help

    Alex

    Code:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    int main () {
    	int i;
    	char name[80];
    	char numbers[10] [80] = {
    		"Alex", "4783748738378",
    		"Anita", "334455235342",
    		"Martin", "015223354666"
    	};
    	
    	cout << "\nPLEASE ENTER THE NAME (q To Quit): ";
    	cin >> name;
    	
    
    		for(i=0; i <= 10; i =+ 2)
    		if(!strcmp(name, numbers[i])) {
    			cout << "The Number Is: " << numbers[i+1] << "\n";
    		}
    	
    	
    	
        return 0;
    }

  2. #2
    Registered User r1ck0r's Avatar
    Join Date
    Apr 2005
    Location
    UK
    Posts
    30
    Code:
    if(strcmp(name,"q") == 0) return 0;

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    Excellent..Thankyou So Much r1ck0r!

    Man I feel Dumb..it was so simple!

    Ill Give u some green for your help!

    Cheers

    Alex

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    if( (strcmp(name,"q")==0) || (strcmp(name,"Q")==0) )return 0;
    Last edited by The Brain; 04-12-2005 at 10:33 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    Thanx The Brain For The suggestion. I actually shoved that in just before reading your post

    Anyways..U can have some green as well..for being a good person! (And also making me sure that the code is sweet!)

    Cheers
    Alex

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    Ok..ive added the suggest line of code..The q to quit now works..however if I enter a name..nothing happens. I think it could be something with the position of the inserted line of code..hummmm

    Code:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    int main () {
    	int i;
    	char name[80];
    	char numbers[10] [80] = {
    		"Alex", "4783748738378",
    		"Anita", "334455235342",
    		"Martin", "015223354666"
    	};
    	
    	cout << "\nPLEASE ENTER THE NAME (q To Quit): ";
    	cin >> name;
    	
    
    		for(i=0; i <= 10; i =+ 2)
    		if( (strcmp(name,"q")==0) || (strcmp(name,"Q")==0) )return 0; 
    	    if(!strcmp(name, numbers[i])) {
    			cout << "The Number Is: " << numbers[i+1] << "\n";
    		}
    			
    			
    	
    	
    	
        return 0;
    }

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You need brackets after the for loop to group together both of those ifs.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    Another Great answer!

    Thanx joshdick!
    (some green also comming your way!)

    Cheers

    Alex

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You're welcome.

  10. #10
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by The Brain
    Code:
    if( (strcmp(name,"q")==0) || (strcmp(name,"Q")==0) )return 0;
    Just use strcmpi(), it's case insensitive...so 'Q' and 'q' would both work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. processes not exiting or quit
    By kryptkat in forum Tech Board
    Replies: 8
    Last Post: 11-13-2006, 12:43 PM
  3. I quit!
    By Luigi in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2002, 09:30 AM
  4. implicit declatation of function 'int toupper(...)'
    By Intimd8r in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 02:43 PM