Re: What's wrong with my code?

  • From: "qubit" <lauraeaves@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Tue, 6 Jul 2010 00:10:24 -0500

I'd make only one addition to Ken's code-- for each prompt that he puts before 
the input, it is a good idea to flush the output stream so the user will see 
the prompt before the input statement is executed:

cout << "please input something here: " << flush;
cin >> varname;

You get the idea.  This buffering of stream io bites most beginning students. 
What happens in buffered output is that the output is accumulated in a buffer 
or character array which is only actually output when the array is full.  So if 
you type one prompt without a flush, the output may be sitting in the buffer 
instead of being output to the screen when execution is waiting for you to type 
the input.

Hope that explains it. Happy hacking.
--le

  ----- Original Message ----- 
  From: Ken Perry 
  To: programmingblind@xxxxxxxxxxxxx 
  Sent: Monday, July 05, 2010 11:14 PM
  Subject: RE: What's wrong with my code?


   

  Jess, 

   

  I will go a bit farther because you have gone so far.  TI re wrote your code 
to do what you wanted please study the differences.  Note that this one 
compiles..  I also changed the int's to floats because your code seems to state 
that the user can input decimal so even if your code worked if a user put 1.5 
in you would get 1 out.  It doesn't round it actually just wipes out the end 
decimal part.  Also in the beginning you don't need all the using statements..  
You only need the one as I did.  I also put prompts on each cin without them 
the user would not know what to do.   So anyway here is a working version of 
your code.

   

  #include <iostream>

   

  using namespace std;

   

  //declare variables

  float score_1=0;

  float score_2=0;

  float score_3 (0);

   

  //begin program

  int main(int argc, char **argv)

  {

                  cout<< "Please enter your first test score. You may enter 
decimal values: " ;

  cin >> score_1 ;

                  cout<< "Please enter your second  test score."; 

  cin >>score_2  ;

                  cout<< "Please enter your third   test score."; 

  cin >>score_3  ;

   

  return (0);

  }

   

   

   

  From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Jes
  Sent: Monday, July 05, 2010 11:55 PM
  To: programmingblind@xxxxxxxxxxxxx
  Subject: What's wrong with my code?

   

  Hi all,

  This is a programming assignment I'm trying to do out of the class text book. 
Any assistance would be appreciated, as well as any feedback on how I am doing 
writing the code. I have this habit of not writing the code all the way 
through, and compiling the program bit by bit to make sure I don't get any 
errors in the process of coding. I just want to make sure that the code I have 
already written is working as it should before I continue writing. I'm sure 
this is not a good habit to get into.

  Thanks for any help.

  Jes

   

  //Ch5 Exercise 4, page 287 //Calculates and displays the average of three 
test scores

  //created/revised by Jes Smith on July 5 2010

   

  #include <iostream>

  using <<std::cout;>>

  using <<std::cin;>>

  using <<std::endl;>>

   

  //declare variables

  int score_1 (0);

  int score_2 (0);

  int score_3 (0);

   

  //begin program

  int main()

  {

                  cout< "Please enter your first test score. You may enter 
decimal values: " >;

  cin << score_1 >>;

  cin <<score_2 >> ;

  cin <<score_3 >> ;

   

  return 0

  }

   

Other related posts: