Question

  • Here the code executes the required output perfectly
  • It checks for the condition and prints the error if the condition is not true , but it also prints even if the condition is true and followed but the required output.
  • finding problem in a small if else condition
  • how it can corrected
  • In the below codeif (fork() == 0) & if (!request.compare("") == 0) has no effect

    /* Loop forever */ 
    while (1) 
    {
    /* Accept a client connection */    
        clientFd = accept (serverFd, clientSockAddrPtr, (socklen_t*)&clientLen); 
    
    //Fork child
        //if (fork() == 0) 
        {
        while(1)
        {   //Reads the client
                string request = read_Request (clientFd); 
            bool found = false;
            string response="Error !!! Country not found"; 
            //if (!request.compare("") == 0) 
            {
    
                for(vector<Country>::iterator it = countryData.begin(); it != countryData.end(); it++)
                {
            if(request.compare(it -> name) == 0) 
            {   
            string countryName = it -> name; 
            response += "\n";               
            response += it -> name; 
            response += "'s TLD Code"; 
            response += ((countryName).append("'s TLD Code")); 
            response += ": ";
            response += it -> TLDCode; 
            response += "\n";
    
            countryName = it -> name;
            response += it -> name;
            response += "'s FIPS104 Country Code"; 
            response += ((countryName).append("'s FIPS104 Country Code")); 
            response += ": ";
            response += it -> FIPS104CountryCode; 
            response += "\n";
    
            countryName = it -> name;
            response += it -> name;
            response += "'s ISO2 Country Code"; 
            response += ((countryName).append("'s ISO2 Country Code")); 
            response += ": ";
            response += it -> ISO2CountryCode; 
            response += "\n";
    
            countryName = it -> name; 
            response += it -> name; 
            response += "'s population"; 
            response += ((countryName).append("'s population")); 
            response += ": ";
            response += it -> population; 
            response += "\n";
    
    
                found = true;
    
                }
            }
    }
    
    
    
    write (clientFd, response.c_str(), strlen (response.c_str()) + 1);  
        }
        }
    
            close (clientFd); 
    }
    

    }

Here is the snip of output

The string response is printed in case of both true and false.

Any suggestions please

Was it helpful?

Solution

Okay so here you assign a string to the variable response
String response="Error !!! Country not found";

then here you concatinate other string to response, which gives you your output.

response += "\n";               
response += it -> name; 

i.e. Error !!! Country not found + \n + it->name etc

Secondly as @ChrisJ.Kiick said you have a mysterious String type which isnt a part of the c language

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top