Thursday, July 30, 2009

How to make this C++ program to indent one line down when the program prompts the second time?

this is my code:


#include %26lt;iostream%26gt;


using std::cout;


using std::cin;


using std::endl;





int main ( )


{


//declare varibles


int days[12] = {31, 28, 31, 30, 31, 30, 31, 30, 30, 31, 30, 31};


int monthNumber =0;





//get input from user


cout %26lt;%26lt; "Enter the month's number: " %26lt;%26lt; endl;


cin %26gt;%26gt; monthNumber;





while (monthNumber != -1)


{


if (monthNumber %26gt;= 1 %26amp;%26amp; monthNumber %26lt;= 12)


cout %26lt;%26lt; "Number of Days: "%26lt;%26lt; days [monthNumber - 1];


else


cout %26lt;%26lt; "Invalid Input" %26lt;%26lt; endl;


//end if


cout %26lt;%26lt; "Enter the month's number: " %26lt;%26lt; endl;


cin %26gt;%26gt; monthNumber;


}


//endwhile


return 0;


}

How to make this C++ program to indent one line down when the program prompts the second time?
Hokay, I ain't run it, so I'm just guessing, but it looks to me like the output would be:





Enter the month's number %26lt;LF%26gt;


%26lt;input%26gt;%26lt;LF%26gt;


Number of Days %26lt;days[%26lt;input%26gt;]%26gt;Enter the month's number:%26lt;LF%26gt;


Is that substantially correct? if so then change the:





cout %26lt;%26lt; "Number of Days: "%26lt;%26lt; days [monthNumber - 1];





to read:





cout %26lt;%26lt; "Number of Days: "%26lt;%26lt; days [monthNumber - 1] %26lt;%26lt; endl;





End of problem.





Edit:


I ran it. Here's my output before the change I suggested:


Enter the month's number:


4


Number of Days: 30Enter the month's number:


5


Number of Days: 31Enter the month's number:


-1





After the change:


Enter the month's number:


4


Number of Days: 30


Enter the month's number:


5


Number of Days: 31


Enter the month's number:


-1





Why don't you just use:





using namespace std;


at the top of your program under the includes? It's much easier than citing individual objects and functions.
Reply:You just need to output the newline character "\n":





change your line:


cout %26lt;%26lt; "Enter the month's number: " %26lt;%26lt; endl;


to


cout%26lt;%26lt;"\n"%26lt;%26lt;"Enter the month's number: "%26lt;%26lt;endl;





You can put the newline character wherever you want.





Another character I like is the tab: "\t". It makes things


a little easier to read.


No comments:

Post a Comment