Thread: Q and A program

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    8

    Q and A program

    hi, i am a beginner at C ++ programming (actually i just started today) but i got the hang of it pretty quick. i am now currently making a program in which i will input strings and the program will respond to my question. my question is how to keep the console window from closing after every question and answer. Right now i am able to ask a question and get a response, but only once. in order to ask a different question i have to reopen the program and type my question again. is there anyway i could just keep inputing strings until i want to manually close the console by closing the window?

  2. #2
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    u have to use loop(while or for),post u r code ,lets see where to modify

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    8
    Code:
    #include <iostream>
    #include <cstring>	
    
    
    using namespace std;
    		
    int main()                            
    {
      char quest [100000]; 
      
      cout<<"Please input your question: ";    
      cin.getline ( quest, 50, '\n' );                          
      
      if ( strcmp ( quest, "how old am i?" ) == 0 ) 
        cout<<"xxx\n";
      
      else if ( strcmp ( quest, "what is my name?" ) == 0 )            
         cout<<"xxx\n";      
         
      else if ( strcmp ( quest, "what is the capital of xxx?" ) == 0 )            
         cout<<"xxx\n";     
         
      else if ( strcmp ( quest, "who is the best xxx?" ) == 0 )            
         cout<<"xxx\n";
         
      else 
        cout<<"I only answer to xxx\n";
        
      
      cin.get();
      return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, have you learned loops yet?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    8
    yea but how do i add them in with this?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, write a program that loops and prints "hello world" in a loop. On each iteration, ask the user if he or she wants to quit. If the user enters yes (e.g., the letter 'Y' or 'y'), then quit. If not, continue with the next "hello world" iteration.

    A slightly more challenging variant of this is to only accept 'Y', 'y', 'N' and 'n' as valid input. If the user enters invalid input, continue asking if the user wants to quit (without printing the next "hello world").

    When you have finished this exercise, you would have a better idea of how to apply this to your current program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    till u type quit u will be asked questions
    Code:
    #include <iostream>
    #include <cstring>	
    
    using namespace std;
    
    int main()
    {
    	char quest [100000];
    
    	while (1)
    	{
    		cout<<"Please input your question: ";
    		cin.getline(quest, 50, '\n');
    
    		if (strcmp(quest, "how old am i?") == 0)
    			cout<<"old xxx\n";
    
    		else if (strcmp(quest, "what is my name?") ==0)
    			cout<<"name xxx\n";
    
    		else if (strcmp(quest, "what is the capital of xxx?") == 0)
    			cout<<"capital xxx\n";
    
    		else if (strcmp(quest, "who is the best xxx?") == 0)
    			cout<<" best xxx\n";
    
    		else if (strcmp(quest, "quit") == 0)
    		{
    			cout<<"bye...";
    			return 0;
    		}
    
    		else
    			cout<<"I only answer to xxx\n";
    
    	}
    
    }

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    8
    Thanks alot you guys, Does anyone know how i could include a time/date or location Q and A. I know java has applets for that type of thing, i found similar stuff for C++ but how do i include it in my program? Thank you in advance

    EDIT*, i resolved the time/date issue, any ideas on location?
    Last edited by LeLouche008; 09-13-2008 at 01:44 PM.

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    What do you mean time/date and location Q and A?

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    8
    well i solved the issue of asking a questions about the time and date and getting an up to date answer. But i still can't find a code that will allow me to determine my current location based on my ip address. I mean there are alot of pop ups and online ads that display your city's name depending on your ip address but i don't know if its possible to incorporate that into a C++ program.

    i tried to use this but since its in chinese i didnt know how and it ended up not working:
    http://www.deanlee.cn/programming/co...phic-location/


    EDIT: also, if there is ANYONE amazing enough to figure out a way in which i could type a term into the console and i would be responded with a at least a partial definition/info from wikipedia or any other online encyclopedia straight into my console, (Artificial intelligence using information from wikipedia) that would be insanely cool. Is this even possible or is it just a crazy dream?
    Last edited by LeLouche008; 09-13-2008 at 06:47 PM.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by LeLouche008 View Post
    EDIT: also, if there is ANYONE amazing enough to figure out a way in which i could type a term into the console and i would be responded with a at least a partial definition/info from wikipedia or any other online encyclopedia straight into my console, (Artificial intelligence using information from wikipedia) that would be insanely cool. Is this even possible or is it just a crazy dream?
    It's possible, but it's no small project. You'd need basic understanding of HTTP, an Networking library, and and HTML parser for starters.

    That'll at least get you the information. Trying to interpret for to create AI is a nother challenge entirely.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    8
    Thanks King Mir, does anyone know how i can open an mp3 file sitting on my desktop with a c++ program. i don't want the program to play it for me, i just want windows media player to do it. same effect as double clicking the file.

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    It's possible, but it's no small project. You'd need basic understanding of HTTP, an Networking library, and and HTML parser for starters.

    That'll at least get you the information. Trying to interpret for to create AI is a nother challenge entirely.
    O_o

    Or, if he only needs a definition, he could use 'DICT'.

    Code = DICT RFC + 30 Minutes

    Yea... Give me a break; I get bored cataloging inventory.

    Soma

    Code:
    #include <winsock.h>
    #include <stdio.h>
    
    const char ServerName[] = "dict.org";
    const unsigned short PortNumber = 2628;
    const unsigned short BufferSize = 6144;
    
    const char InitialResponse[] = "220";
    
    const char QuitResponse[] = "221";
    const char QuitCommand[] = "QUIT\r\n";
    
    const char DefineResponse[] = "150";
    const char DefinitionResponse[] = "151";
    const char FinishedResponse[] = "250";
    const char DefineCommand[] = "DEFINE";
    
    int main(int argumentcount, char ** arguments)
    {
    	WSADATA winsock;
    	WSAStartup(MAKEWORD(2,0), &winsock);
    	SOCKET client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	LPHOSTENT host = gethostbyname(ServerName);
    	SOCKADDR_IN address = {AF_INET, htons(PortNumber), *((LPIN_ADDR)*host->h_addr_list)};
    	connect(client, (LPSOCKADDR)(&address), sizeof(address));
    	char * commandBuffer = (char *)malloc(BufferSize);
    	char * responseBuffer = (char *)malloc(BufferSize);
    	char * command = commandBuffer;
    	char * response = responseBuffer;
    	recv(client, response, BufferSize, 0);
    	memcmp(response, InitialResponse, sizeof(InitialResponse) - 1);
    	sprintf(command, "%s ! %s\r\n", DefineCommand, arguments[1]);
    	send(client, command, strlen(command), 0);
    	recv(client, response, BufferSize, 0);
    	memcmp(response, DefineResponse, sizeof(DefineResponse) - 1);
    	unsigned int definitionCount = atoi(response + 4);
    	recv(client, response, BufferSize, 0);
    	char * delimiter = strstr(response, "\r\n.\r\n250");
    	char * definition = (char *)malloc(BufferSize);
    	delimiter = strstr(response, "\r\n");
    	snprintf(definition, BufferSize, "%s", delimiter + 2);
    	delimiter = strstr(definition, "\r\n.\r\n");
    	*delimiter = 0;
    	printf("%s\n", definition);
    	delimiter = strstr(response, "\r\n.\r\n") + 5;
    	response = delimiter;
    	free(definition);
    	memcmp(response, FinishedResponse, sizeof(FinishedResponse) - 1);
    	response = responseBuffer;
    	send(client, QuitCommand, sizeof(QuitCommand) - 1, 0);
    	recv(client, response, BufferSize, 0);
    	memcmp(response, QuitResponse, sizeof(QuitResponse) - 1);
    	free(commandBuffer);
    	free(responseBuffer);
    	closesocket(client);
    	WSACleanup();
    	return(0);
    }

  14. #14
    Registered User
    Join Date
    Sep 2008
    Posts
    8
    Quote Originally Posted by phantomotap View Post
    O_o

    Or, if he only needs a definition, he could use 'DICT'.

    Code = DICT RFC + 30 Minutes

    Yea... Give me a break; I get bored cataloging inventory.

    Soma

    Code:
    #include <winsock.h>
    #include <stdio.h>
    
    const char ServerName[] = "dict.org";
    const unsigned short PortNumber = 2628;
    const unsigned short BufferSize = 6144;
    
    const char InitialResponse[] = "220";
    
    const char QuitResponse[] = "221";
    const char QuitCommand[] = "QUIT\r\n";
    
    const char DefineResponse[] = "150";
    const char DefinitionResponse[] = "151";
    const char FinishedResponse[] = "250";
    const char DefineCommand[] = "DEFINE";
    
    int main(int argumentcount, char ** arguments)
    {
    	WSADATA winsock;
    	WSAStartup(MAKEWORD(2,0), &winsock);
    	SOCKET client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	LPHOSTENT host = gethostbyname(ServerName);
    	SOCKADDR_IN address = {AF_INET, htons(PortNumber), *((LPIN_ADDR)*host->h_addr_list)};
    	connect(client, (LPSOCKADDR)(&address), sizeof(address));
    	char * commandBuffer = (char *)malloc(BufferSize);
    	char * responseBuffer = (char *)malloc(BufferSize);
    	char * command = commandBuffer;
    	char * response = responseBuffer;
    	recv(client, response, BufferSize, 0);
    	memcmp(response, InitialResponse, sizeof(InitialResponse) - 1);
    	sprintf(command, "%s ! %s\r\n", DefineCommand, arguments[1]);
    	send(client, command, strlen(command), 0);
    	recv(client, response, BufferSize, 0);
    	memcmp(response, DefineResponse, sizeof(DefineResponse) - 1);
    	unsigned int definitionCount = atoi(response + 4);
    	recv(client, response, BufferSize, 0);
    	char * delimiter = strstr(response, "\r\n.\r\n250");
    	char * definition = (char *)malloc(BufferSize);
    	delimiter = strstr(response, "\r\n");
    	snprintf(definition, BufferSize, "%s", delimiter + 2);
    	delimiter = strstr(definition, "\r\n.\r\n");
    	*delimiter = 0;
    	printf("%s\n", definition);
    	delimiter = strstr(response, "\r\n.\r\n") + 5;
    	response = delimiter;
    	free(definition);
    	memcmp(response, FinishedResponse, sizeof(FinishedResponse) - 1);
    	response = responseBuffer;
    	send(client, QuitCommand, sizeof(QuitCommand) - 1, 0);
    	recv(client, response, BufferSize, 0);
    	memcmp(response, QuitResponse, sizeof(QuitResponse) - 1);
    	free(commandBuffer);
    	free(responseBuffer);
    	closesocket(client);
    	WSACleanup();
    	return(0);
    }
    really cool man! but it won't run for me for some some reason :/

  15. #15
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    really cool man! but it won't run for me for some some reason :/
    Yea... bored... not completely insane.

    You'll actually have to insert tests if you want to find problems.

    Soma

Popular pages Recent additions subscribe to a feed