0

enter image description here

Here is my code : Code for main function:

switch(ch){

                case 1:{
                // Call the function two times to store the result from color function
                             b = color(BAND_COLOR_CODE, band); 

                            c = color(BAND_COLOR_CODE, band); 

                            cout << "\n"; 

                            //Call the function to store the result from the calculation
                           ---------code here------

                            //Call the function to store the tolerance 
                            -------code here-------
                            ------code---------
                           else{



                            //Display information about the resistor
                            cout << "Results:\n" << "R="<<product << "ohms \n" <<"Tolerance=" <<output <<"%\n"<<"Rmax="<<x<<"\nRmin="<<y<< endl; 

                            cout << "\n";

                            }
                break;
            }

Code snippet for color function

    int color(string BAND_COLOR_CODE[], const int band)
    {

        static int j = 64;   // Stores user input for color 
        int i=0;
        ++j;                // Increments to allow user to input colors in succession 


        // Prompt for user input 
         cout << "Please enter a color for band " << (char)j << ": >"; 
        cin.getline(code,10); 
         cout<<"\n";
        // Loop to take care of the case of letters 
       -----------
----------code here------
-----code here-----
}

So basically in my switch case i am calling the function color() two times but as you can see that i am unable to give input on my output screen as the function prints Please enter a color for band A:> Please enter a color for band B:> simultaneously without allowing me to give the input for band A . though i am able to give the input for band b. I don't understand the reason for this anomaly in my code Please help.

  • Please, add the code regarding the first choice. – Bob__ Apr 27 '17 at 06:10
  • 1
    You may want to read http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction to get an understanding of how leftover whitespace in the input stream buffer can cause this type of behaviour. My guess is that the first call to `color` consumes a leftover newline from the code that lets the user choose between "choice1" and "choice2". – sigbjornlo Apr 27 '17 at 06:11
  • @Bob_ the code for choice 1 is the code in switch case 1 snippet – Vimal Bhardwaj Apr 27 '17 at 06:25
  • 2
    @VimalBhardwaj The code where the user _chooses_ choice 1 (before the switch statement presumably) is likely part of the problem and should be part of the question. Try commenting out the cin before the switch and instead hardcoding the choice so that you will always go into case 1. Does that change the behaviour of the program? – sigbjornlo Apr 27 '17 at 06:32
  • @sigbjornlo hey if i comment out the cin then how am i supposed to execute the other module.can you please explain me little more. – Vimal Bhardwaj Apr 27 '17 at 06:41
  • 1
    @VimalBhardwaj Sorry if I was being unclear. I was suggesting you comment it temporarily only to let you see that the input taken in the color function will work as intended when there is no whitespace left over in the input stream from previous use of cin's operator >>. Search for "mixing cin and getline" for a number of people with similar issues to you (and a number of different solutions). – sigbjornlo Apr 27 '17 at 06:49
  • 1
    http://stackoverflow.com/questions/8332144/cin-not-work-with-getline?rq=1 http://stackoverflow.com/questions/32365277/the-role-of-stdws-whitespace-when-reading-data offer two different solutions. – sigbjornlo Apr 27 '17 at 06:53
  • @sigbjornlo Well its working fine when i am not using a switch case. – Vimal Bhardwaj Apr 27 '17 at 06:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142792/discussion-between-sigbjornlo-and-vimal-bhardwaj). – sigbjornlo Apr 27 '17 at 07:01
  • 1
    @sigbjornlo hey thanks for the advice it working fine now after using " std::cin >> std::ws;" there was some problem related to newline.But the post you suggested helped me to solve it. thanks man – Vimal Bhardwaj Apr 27 '17 at 07:07

0 Answers0