Saturday, May 22, 2010

C++ Parrallel Questions?

I want to output a messege saying there is no such name if the name is not in the array. How do I do that? Also when I output it on the screen I get the correct numbers but they are all together no space between them. How do I correct that?











Here is a snippet of my code:





cout %26lt;%26lt; " What is the name of the player whose scores you wish to see? " %26lt;%26lt; endl;


cin %26gt;%26gt; name;








for (int i = 0; i %26lt; noOfNames; i++)


if (userName[i] == name)


cout %26lt;%26lt; score[i] %26lt;%26lt; " ";








It does not do the " "; part.





Sample output:





52146





when the scores are 5, 21, 46








Thanks for the help. If you need any more information I will provide it if I can.

C++ Parrallel Questions?
If you want the commas before the space, you will have to modify your loop. Also, if you want to print out a special message if no instances are found, you should created an extra variable to track if the name was found





this is a quick sample, but has not been tested.





bool instancesFound = false;





for (int i = 0; i %26lt; noOfNames; i++){


if (userName[i] == name){





//if a number was printed before this, print a comma and a space.


if(instancesFound){


cout %26lt;%26lt; ", ";


}


instances found = true;


cout %26lt;%26lt; score[i];


}


}


if ( !instancesFound ){


cout %26lt;%26lt; "none found" %26lt;%26lt; endl;


}
Reply:Add a variable





bool found=false;





for (int i = 0; i %26lt; noOfNames; i++)


if(userName[i]==name}


{


found=true;


cout %26lt;%26lt; score %26lt;%26lt; " ";


}





if(!found) cout %26lt;%26lt; "Not found";

sound cards

No comments:

Post a Comment