Here's my code:
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
int i = 0, max = 0, sum = 0;
char indicator = 'y';
cout%26lt;%26lt;"Please enter number 5: "; cin%26gt;%26gt;max;
cout%26lt;%26lt;endl;
for(i=1; i%26lt;=max; i++)
sum+=i;
cout%26lt;%26lt;endl
%26lt;%26lt;"The Sum is = "%26lt;%26lt;sum%26lt;%26lt;endl;
cout%26lt;%26lt;"Do you wish to display the odd sum?"%26lt;%26lt;endl;
cout%26lt;%26lt;"Enter y for yes, (n to end)"; cin%26gt;%26gt;indicator;
if(indicator=='y')
{
for(i=1; max%26lt;=max; i=1+2)
sum=sum+i;
cout%26lt;%26lt;endl
%26lt;%26lt;"The odd sum is = "%26lt;%26lt;sum%26lt;%26lt;endl;
}
cout%26lt;%26lt;"Okay Bye"%26lt;%26lt;endl;
return 0;
}
The answer for the 1st sum is correct 15
But the Odd sum answer should be 9 and it's aint displaying and wrong answer.
C++ help me please?
You forgot to re-initialize sum. It's adding 9 to 15 and giving you 24, right? And yeah, I missed it, but you also need to have i = i + 2, not i = 1 + 2.
if(indicator=='y')
{
sum = 0;
for(i=1; max%26lt;=max; i=i+2)
sum=sum+i;
cout%26lt;%26lt;endl
%26lt;%26lt;"The odd sum is = "%26lt;%26lt;sum%26lt;%26lt;endl;
}
Reply:for(i=1; max%26lt;=max; i=1+2)
That is very wrong there, check your study material what is not right.
Reply:I never got to C++, but do you want i=3 or do you want i=i+2?
Your line is:
for(i=1; max%26lt;=max; i=1+2)
, which would always make i=3.
Reply:Yeah, i have to agree with some of the other answers.
sum needs to be reinitialized to 0 before the second for loop.
the second for loop looks funky to me. Especially the condition max%26lt;=max and i=1+2.
Reply:Two problems:
You need to add a line that zeros your sum for the second accumulation:
if(indicator=='y')
{
sum=0; // NEW LINE TO RESET SUM
for(i=1; max%26lt;=max; i=1+2)
And the line :
for(i=1; max%26lt;=max; i=1+2)
Needs to be re-written:
for(i=1;i%26lt;=max;i+=2) // NOT max%26lt;=max and NOT i=1+2
Sunday, August 2, 2009
C++ help me please?
Here's my code:
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
int i = 0, max = 0, sum = 0;
char indicator = 'y';
cout%26lt;%26lt;"Please enter number 5: "; cin%26gt;%26gt;max;
cout%26lt;%26lt;endl;
for(i=1; i%26lt;=max; i++)
sum+=i;
cout%26lt;%26lt;endl
%26lt;%26lt;"The Sum is = "%26lt;%26lt;sum%26lt;%26lt;endl;
cout%26lt;%26lt;"Do you wish to display the odd sum?"%26lt;%26lt;endl;
cout%26lt;%26lt;"Enter y for yes, (n to end)"; cin%26gt;%26gt;indicator;
if(indicator=='y')
{
for(i=1; max%26lt;=max; i=1+2)
sum=sum+i;
cout%26lt;%26lt;endl
%26lt;%26lt;"The odd sum is = "%26lt;%26lt;sum%26lt;%26lt;endl;
}
cout%26lt;%26lt;"Okay Bye"%26lt;%26lt;endl;
return 0;
}
The answer for the 1st sum is correct 15
But the Odd sum answer should be 9 and it's aint displaying and wrong answer.
C++ help me please?
You forgot to re-initialize sum. It's adding 9 to 15 and giving you 24, right? And yeah, I missed it, but you also need to have i = i + 2, not i = 1 + 2.
if(indicator=='y')
{
sum = 0;
for(i=1; max%26lt;=max; i=i+2)
sum=sum+i;
cout%26lt;%26lt;endl
%26lt;%26lt;"The odd sum is = "%26lt;%26lt;sum%26lt;%26lt;endl;
}
Reply:for(i=1; max%26lt;=max; i=1+2)
That is very wrong there, check your study material what is not right.
Reply:I never got to C++, but do you want i=3 or do you want i=i+2?
Your line is:
for(i=1; max%26lt;=max; i=1+2)
, which would always make i=3.
Reply:Yeah, i have to agree with some of the other answers.
sum needs to be reinitialized to 0 before the second for loop.
the second for loop looks funky to me. Especially the condition max%26lt;=max and i=1+2.
Reply:Two problems:
You need to add a line that zeros your sum for the second accumulation:
if(indicator=='y')
{
sum=0; // NEW LINE TO RESET SUM
for(i=1; max%26lt;=max; i=1+2)
And the line :
for(i=1; max%26lt;=max; i=1+2)
Needs to be re-written:
for(i=1;i%26lt;=max;i+=2) // NOT max%26lt;=max and NOT i=1+2
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
int i = 0, max = 0, sum = 0;
char indicator = 'y';
cout%26lt;%26lt;"Please enter number 5: "; cin%26gt;%26gt;max;
cout%26lt;%26lt;endl;
for(i=1; i%26lt;=max; i++)
sum+=i;
cout%26lt;%26lt;endl
%26lt;%26lt;"The Sum is = "%26lt;%26lt;sum%26lt;%26lt;endl;
cout%26lt;%26lt;"Do you wish to display the odd sum?"%26lt;%26lt;endl;
cout%26lt;%26lt;"Enter y for yes, (n to end)"; cin%26gt;%26gt;indicator;
if(indicator=='y')
{
for(i=1; max%26lt;=max; i=1+2)
sum=sum+i;
cout%26lt;%26lt;endl
%26lt;%26lt;"The odd sum is = "%26lt;%26lt;sum%26lt;%26lt;endl;
}
cout%26lt;%26lt;"Okay Bye"%26lt;%26lt;endl;
return 0;
}
The answer for the 1st sum is correct 15
But the Odd sum answer should be 9 and it's aint displaying and wrong answer.
C++ help me please?
You forgot to re-initialize sum. It's adding 9 to 15 and giving you 24, right? And yeah, I missed it, but you also need to have i = i + 2, not i = 1 + 2.
if(indicator=='y')
{
sum = 0;
for(i=1; max%26lt;=max; i=i+2)
sum=sum+i;
cout%26lt;%26lt;endl
%26lt;%26lt;"The odd sum is = "%26lt;%26lt;sum%26lt;%26lt;endl;
}
Reply:for(i=1; max%26lt;=max; i=1+2)
That is very wrong there, check your study material what is not right.
Reply:I never got to C++, but do you want i=3 or do you want i=i+2?
Your line is:
for(i=1; max%26lt;=max; i=1+2)
, which would always make i=3.
Reply:Yeah, i have to agree with some of the other answers.
sum needs to be reinitialized to 0 before the second for loop.
the second for loop looks funky to me. Especially the condition max%26lt;=max and i=1+2.
Reply:Two problems:
You need to add a line that zeros your sum for the second accumulation:
if(indicator=='y')
{
sum=0; // NEW LINE TO RESET SUM
for(i=1; max%26lt;=max; i=1+2)
And the line :
for(i=1; max%26lt;=max; i=1+2)
Needs to be re-written:
for(i=1;i%26lt;=max;i+=2) // NOT max%26lt;=max and NOT i=1+2
C++ Needs help with my code?
My current problem is i wanted the user to type I am great, and save it under a txt file. but somehow it only display "I"
#include %26lt;iostream%26gt;
#include %26lt;fstream%26gt;
#include %26lt;string%26gt;
using namespace std;
int main()
{
void Enter();
Enter();
return 0;
}
void Enter()
{
void Done();
ofstream fout;
string szLine ="";
string szWord = "";
string szName = "";
string szType ="";
//Starting//
cout%26lt;%26lt;"Please enter your name: "; cin%26gt;%26gt;szName;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
cout%26lt;%26lt;"Please enter the follow sentence: I am Great."; cin%26gt;%26gt;szType;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
fout.open("Stats.txt");
fout %26lt;%26lt; "Name: " %26lt;%26lt; szName %26lt;%26lt; endl;
fout %26lt;%26lt; "Typed: " %26lt;%26lt; szType %26lt;%26lt; endl;
fout.close();
Done();
}
void Done()
{
cout%26lt;%26lt;"Thanks check the text."%26lt;%26lt;endl;
}
C++ Needs help with my code?
You need to have a prototype for the functions inside the main(). It should be written before the main. To resolve it quickly, put the main function at the bottom part.
OR: edit the code
#include %26lt;iostream%26gt;
#include %26lt;fstream%26gt;
#include %26lt;string%26gt;
using namespace std;
void Done(); // prototype for function Done()
void Enter(); // prototype for function Enter()
int main()
{
void Enter();
Enter();
return 0;
}
then: use cin.getline(); instead of cin only
Reply:Very few people here knows how to code, so I recommend finding a good programmer's forum or IRC channel. Do a google search for "programmer's forum".
Reply:when you have a sub that's getting called in the main, you need to write its code before the main.
Reply:OK, here is your problem. cin implicitly parses your input according to spaces and a few other delimiters.
So when you cin to a string, you are getting only the first word of your string. "I am great" will only capture "I" on one cin.
In order to capture all text before the user hits enter, use this:
cout %26lt;%26lt; "Please enter the following sentence: I am Great";
getline(cin, szType);
EDIT: the delimiters I mentioned before are space, tab, and newline ( http://www.cplusplus.com/doc/tutorial/ba... )
Reply:don't have time right now, but here is a link that will teach you:
http://cprogramming.com/
#include %26lt;iostream%26gt;
#include %26lt;fstream%26gt;
#include %26lt;string%26gt;
using namespace std;
int main()
{
void Enter();
Enter();
return 0;
}
void Enter()
{
void Done();
ofstream fout;
string szLine ="";
string szWord = "";
string szName = "";
string szType ="";
//Starting//
cout%26lt;%26lt;"Please enter your name: "; cin%26gt;%26gt;szName;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
cout%26lt;%26lt;"Please enter the follow sentence: I am Great."; cin%26gt;%26gt;szType;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
fout.open("Stats.txt");
fout %26lt;%26lt; "Name: " %26lt;%26lt; szName %26lt;%26lt; endl;
fout %26lt;%26lt; "Typed: " %26lt;%26lt; szType %26lt;%26lt; endl;
fout.close();
Done();
}
void Done()
{
cout%26lt;%26lt;"Thanks check the text."%26lt;%26lt;endl;
}
C++ Needs help with my code?
You need to have a prototype for the functions inside the main(). It should be written before the main. To resolve it quickly, put the main function at the bottom part.
OR: edit the code
#include %26lt;iostream%26gt;
#include %26lt;fstream%26gt;
#include %26lt;string%26gt;
using namespace std;
void Done(); // prototype for function Done()
void Enter(); // prototype for function Enter()
int main()
{
void Enter();
Enter();
return 0;
}
then: use cin.getline(); instead of cin only
Reply:Very few people here knows how to code, so I recommend finding a good programmer's forum or IRC channel. Do a google search for "programmer's forum".
Reply:when you have a sub that's getting called in the main, you need to write its code before the main.
Reply:OK, here is your problem. cin implicitly parses your input according to spaces and a few other delimiters.
So when you cin to a string, you are getting only the first word of your string. "I am great" will only capture "I" on one cin.
In order to capture all text before the user hits enter, use this:
cout %26lt;%26lt; "Please enter the following sentence: I am Great";
getline(cin, szType);
EDIT: the delimiters I mentioned before are space, tab, and newline ( http://www.cplusplus.com/doc/tutorial/ba... )
Reply:don't have time right now, but here is a link that will teach you:
http://cprogramming.com/
C++ help for someone using the program for the first time today?
Okay, I need some help with "if" statements.
I'm trying to get across to the program...
if theMonth == 4
AND
theDate %26gt;= 1 %26amp;%26amp; theDate %26lt;=19
THEN
(say a statement)
I've tried it as...
cout %26lt;%26lt; "So, what day where you born?" %26lt;%26lt; endl;
int theDate;
cin %26gt;%26gt; theDate;
if((theMonth==4)%26amp;%26amp;(theDate %26gt;=1 %26amp;%26amp; theDate %26lt;= 19))
cout %26lt;%26lt; "I didn't know you were an Aries!" %26lt;%26lt; endl;
But it isn't working! All that happens is when it's executed, it shows..
"So, what day where you born?"
And you input a number between 1 and 19, it asks you to hit another key to close the program.
For the record, earlier in the coding, I did...
int theMonth
cout %26lt;%26lt; "What month were you born?";
cin %26gt;%26gt; theMonth
(switch/case stuff)
cout %26lt;%26lt; "Ah, so you were born in (month)".
I follow the instructions correctly. Input four, get it to say April. Then later I input a number like 15 for the date, and it doesn't work.
Help?
C++ help for someone using the program for the first time today?
EDIT: Just seen your full program - birthMonth is a char and therefore fails the test
if(birthMonth == 4) .
Change the condition to if(birthMonth == '4')
This is a working version of your program:
#include %26lt;conio.h%26gt;
#include %26lt;iostream.h%26gt;
void main()
{
int theMonth, theDate;
cout %26lt;%26lt; "So, what month where you born?" %26lt;%26lt; endl;
cin %26gt;%26gt; theMonth;
cout %26lt;%26lt; "So, what day where you born?" %26lt;%26lt; endl;
cin %26gt;%26gt; theDate;
if((theMonth==4)%26amp;%26amp;(theDate %26gt;=1) %26amp;%26amp; (theDate %26lt;= 19)) {
cout %26lt;%26lt; "I didn't know you were an Aries!" %26lt;%26lt; endl;
}
getch();
}
It's always best to put brackets around if and else statements by habit like this:
if(month == 4)
{
// code
}
else
{
// code
}
Reply:On your If statement be sure you have { open brackets after the criteria and } at the end. I did not see that unless your using ( and ). Which is not the correct symbol for the if statement to be closed in the code.
Reply:not enough information. Doesn't look like C++ in the first place. You'll need to post the actual code (copy/paste) and then you need to say what compiler you are using and what operating system and so on.
The IF statement must be followed by a complete boolean expression in parentheses. In one of your code snippets you have two expressions following the IF... I'm surprised it compiles that way.
If you have a choice, switch to C# right away. It is a much better language, and it will catch programming errors like the one I mention. It won't compile that and you'll know right away what you did wrong. You are missing brackets and so on... and C++ is doing exactly what you told it to do... basically nothing. You don't have a code block after the IF....
marguerite
I'm trying to get across to the program...
if theMonth == 4
AND
theDate %26gt;= 1 %26amp;%26amp; theDate %26lt;=19
THEN
(say a statement)
I've tried it as...
cout %26lt;%26lt; "So, what day where you born?" %26lt;%26lt; endl;
int theDate;
cin %26gt;%26gt; theDate;
if((theMonth==4)%26amp;%26amp;(theDate %26gt;=1 %26amp;%26amp; theDate %26lt;= 19))
cout %26lt;%26lt; "I didn't know you were an Aries!" %26lt;%26lt; endl;
But it isn't working! All that happens is when it's executed, it shows..
"So, what day where you born?"
And you input a number between 1 and 19, it asks you to hit another key to close the program.
For the record, earlier in the coding, I did...
int theMonth
cout %26lt;%26lt; "What month were you born?";
cin %26gt;%26gt; theMonth
(switch/case stuff)
cout %26lt;%26lt; "Ah, so you were born in (month)".
I follow the instructions correctly. Input four, get it to say April. Then later I input a number like 15 for the date, and it doesn't work.
Help?
C++ help for someone using the program for the first time today?
EDIT: Just seen your full program - birthMonth is a char and therefore fails the test
if(birthMonth == 4) .
Change the condition to if(birthMonth == '4')
This is a working version of your program:
#include %26lt;conio.h%26gt;
#include %26lt;iostream.h%26gt;
void main()
{
int theMonth, theDate;
cout %26lt;%26lt; "So, what month where you born?" %26lt;%26lt; endl;
cin %26gt;%26gt; theMonth;
cout %26lt;%26lt; "So, what day where you born?" %26lt;%26lt; endl;
cin %26gt;%26gt; theDate;
if((theMonth==4)%26amp;%26amp;(theDate %26gt;=1) %26amp;%26amp; (theDate %26lt;= 19)) {
cout %26lt;%26lt; "I didn't know you were an Aries!" %26lt;%26lt; endl;
}
getch();
}
It's always best to put brackets around if and else statements by habit like this:
if(month == 4)
{
// code
}
else
{
// code
}
Reply:On your If statement be sure you have { open brackets after the criteria and } at the end. I did not see that unless your using ( and ). Which is not the correct symbol for the if statement to be closed in the code.
Reply:not enough information. Doesn't look like C++ in the first place. You'll need to post the actual code (copy/paste) and then you need to say what compiler you are using and what operating system and so on.
The IF statement must be followed by a complete boolean expression in parentheses. In one of your code snippets you have two expressions following the IF... I'm surprised it compiles that way.
If you have a choice, switch to C# right away. It is a much better language, and it will catch programming errors like the one I mention. It won't compile that and you'll know right away what you did wrong. You are missing brackets and so on... and C++ is doing exactly what you told it to do... basically nothing. You don't have a code block after the IF....
marguerite
C++ question, I know someone out there knows?
int main ( )
{
InputData();
while(CostOfPurchase != -1)
{
CHANGE();
OutputData();
}
return 0;
}
void InputData()
{
cout%26lt;%26lt;"Enter cost of purchase : ";
cin%26gt;%26gt; CostOfPurchase;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
cout%26lt;%26lt;"Enter amount tendered : ";
cin%26gt;%26gt; AmountTendered;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
}
void CHANGE()
{
FinalChange= AmountTendered - CostOfPurchase;
(int)dollar= (FinalChange + 0.00001)/1.00;
change = (FinalChange + 0.00001) - dollar;
(int)quarter= change/0.25;
change = change - .25*quarter;
(int)nickel= change/0.05;
change = change - .05 * nickel;
(int)penny = change/0.01;
}
void OutputData()
{
cout%26lt;%26lt; fixed%26lt;%26lt;showpoint%26lt;%26lt;setprecision...
cout%26lt;%26lt;"The change is $"%26lt;%26lt; FinalChange %26lt;%26lt;endl;
cout%26lt;%26lt; dollar%26lt;%26lt;" dollar"%26lt;%26lt;endl;
cout%26lt;%26lt; quarter%26lt;%26lt;" quarter"%26lt;%26lt;endl;
cout%26lt;%26lt; nickel%26lt;%26lt; " nickel"%26lt;%26lt;endl;
cout%26lt;%26lt; penny%26lt;%26lt; " penny" %26lt;%26lt;endl%26lt;%26lt;endl;
}
C++ question, I know someone out there knows?
it is saying that you are going to be a math person or something to do with money...like a bank...you are counting change, and you end up with a "dollar" "quarter" "nickle" "penny" so youreally end up with $1.31 all together...CORRECT?? probably not...oh well im bored..hah
Reply:like i said earlier you must declare cost of purchase with a value so the loop is entered then you input data in the loop
int main ( )
{
double CostOfPurcase = 0;
//YOU HAVE TO DECLARE THIS VARIABLE HERE AND IT HAS TO BE = 0. TAKE ALL OTHER DECLARATIONS OUT EXCEPT THIS ONE!!!
while(CostOfPurchase != -1)
{
inputData();
CHANGE();
OutputData();
}
return 0;
}
void InputData()
{
cout%26lt;%26lt;"Enter cost of purchase : ";
cin%26gt;%26gt; CostOfPurchase;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
cout%26lt;%26lt;"Enter amount tendered : ";
cin%26gt;%26gt; AmountTendered;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
}
if you are still doing
#include%26lt;iostream%26gt;
#include%26lt;iomanip%26gt;
using namespace std;
int dollar, quarter, nickel,penny;
double CostOfPurchase, AmountTendered, change, CHANGE;
int main ( )
{
cout%26lt;%26lt;"Enter cost of Purchase: "%26lt;%26lt;endl;
cin%26gt;%26gt; CostOfPurchase;
cout%26lt;%26lt;endl;
cout%26lt;%26lt;"Enter amount tendered: "%26lt;%26lt;endl;
cin%26gt;%26gt; AmountTendered;
cout%26lt;%26lt;endl;
that is WRONG DECLARE COST OFPURCAHSE IN YOUR MAIN FUNCTION WITH A VALUE THAT IS NOT == to -1 so the loop is entered and input can take place
Reply:Are you declaring the functions in a header file before you use them? If not either make a header file, or put the functions before the main function, so that main is last. This way the compiler can find them before the main function calls them.
Reply:you're wrong, we don't know unless you actually ask us a question - you simply said 'I know someone out there knows?' then pasted a bunch of source code.
we don't possess psychic abilities so unless you explain what the question is you're not going to have any luck.
are you trying to say there's an error in the code? you don't know what the code does? how the code could be improved? what?
Reply:No they don't, because you forgot to ask the question.
Reply:It needs to be in the InputData function:
cout%26lt;%26lt;"Enter cost of purchase : ";
cin%26gt;%26gt; CostOfPurchase;
RIGHT HERE
cout%26lt;%26lt;endl%26lt;%26lt;endl;
---------------------------------
Put an if statement RIGHT HERE, checking the value of CostOfPurchase. If it equals -1, then exit the program.
Reply:i use the exit(); command to just end the programs, however im using the customized compiler of C++ so im not sure if yours is the same... it shold be. in the while loop add if(answer == -1) exit();
Reply:I answered this question a few minutes ago, reposting is frowned upon, you should have editted your first question. I will put my answer here along with the additional question you have to answer.
You need to enter it in InputData() just before the first line. Also to break out of the void function without entering Amount Tendered, you need a return statement. So it should look like this:
void InputData()
{
while (CostOfPurchase != -1)
{
cout%26lt;%26lt;"Enter cost of purchase : ";
cin%26gt;%26gt; CostOfPurchase;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
}
return;
}
You will have to create a separate function for getting Amount Tendered or else it will never reach there.
At least that's what it seems like your asking
Bet of Luck!
Reply:so umm what's your question exactly?
int main ( )
{
while(CostOfPurchase != -1)
{
InputData();
}
return 0;
}
void InputData()
{
cout%26lt;%26lt;"Enter cost of purchase : ";
cin%26gt;%26gt; CostOfPurchase;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
if(CostOfPurchase!=-1)
{
cout%26lt;%26lt;"Enter amount tendered : ";
cin%26gt;%26gt; AmountTendered;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
CHANGE();
OutputData();
}
}
PS I'm assuming CostOfPurchase is a "global" variable, bad practice btw.
Reply:Maybe this:
int main ( )
{
InputData();
while(CostOfPurchase != -1)
{
CHANGE();
OutputData();
InputData(); //I just added this
}
return 0;
}
{
InputData();
while(CostOfPurchase != -1)
{
CHANGE();
OutputData();
}
return 0;
}
void InputData()
{
cout%26lt;%26lt;"Enter cost of purchase : ";
cin%26gt;%26gt; CostOfPurchase;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
cout%26lt;%26lt;"Enter amount tendered : ";
cin%26gt;%26gt; AmountTendered;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
}
void CHANGE()
{
FinalChange= AmountTendered - CostOfPurchase;
(int)dollar= (FinalChange + 0.00001)/1.00;
change = (FinalChange + 0.00001) - dollar;
(int)quarter= change/0.25;
change = change - .25*quarter;
(int)nickel= change/0.05;
change = change - .05 * nickel;
(int)penny = change/0.01;
}
void OutputData()
{
cout%26lt;%26lt; fixed%26lt;%26lt;showpoint%26lt;%26lt;setprecision...
cout%26lt;%26lt;"The change is $"%26lt;%26lt; FinalChange %26lt;%26lt;endl;
cout%26lt;%26lt; dollar%26lt;%26lt;" dollar"%26lt;%26lt;endl;
cout%26lt;%26lt; quarter%26lt;%26lt;" quarter"%26lt;%26lt;endl;
cout%26lt;%26lt; nickel%26lt;%26lt; " nickel"%26lt;%26lt;endl;
cout%26lt;%26lt; penny%26lt;%26lt; " penny" %26lt;%26lt;endl%26lt;%26lt;endl;
}
C++ question, I know someone out there knows?
it is saying that you are going to be a math person or something to do with money...like a bank...you are counting change, and you end up with a "dollar" "quarter" "nickle" "penny" so youreally end up with $1.31 all together...CORRECT?? probably not...oh well im bored..hah
Reply:like i said earlier you must declare cost of purchase with a value so the loop is entered then you input data in the loop
int main ( )
{
double CostOfPurcase = 0;
//YOU HAVE TO DECLARE THIS VARIABLE HERE AND IT HAS TO BE = 0. TAKE ALL OTHER DECLARATIONS OUT EXCEPT THIS ONE!!!
while(CostOfPurchase != -1)
{
inputData();
CHANGE();
OutputData();
}
return 0;
}
void InputData()
{
cout%26lt;%26lt;"Enter cost of purchase : ";
cin%26gt;%26gt; CostOfPurchase;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
cout%26lt;%26lt;"Enter amount tendered : ";
cin%26gt;%26gt; AmountTendered;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
}
if you are still doing
#include%26lt;iostream%26gt;
#include%26lt;iomanip%26gt;
using namespace std;
int dollar, quarter, nickel,penny;
double CostOfPurchase, AmountTendered, change, CHANGE;
int main ( )
{
cout%26lt;%26lt;"Enter cost of Purchase: "%26lt;%26lt;endl;
cin%26gt;%26gt; CostOfPurchase;
cout%26lt;%26lt;endl;
cout%26lt;%26lt;"Enter amount tendered: "%26lt;%26lt;endl;
cin%26gt;%26gt; AmountTendered;
cout%26lt;%26lt;endl;
that is WRONG DECLARE COST OFPURCAHSE IN YOUR MAIN FUNCTION WITH A VALUE THAT IS NOT == to -1 so the loop is entered and input can take place
Reply:Are you declaring the functions in a header file before you use them? If not either make a header file, or put the functions before the main function, so that main is last. This way the compiler can find them before the main function calls them.
Reply:you're wrong, we don't know unless you actually ask us a question - you simply said 'I know someone out there knows?' then pasted a bunch of source code.
we don't possess psychic abilities so unless you explain what the question is you're not going to have any luck.
are you trying to say there's an error in the code? you don't know what the code does? how the code could be improved? what?
Reply:No they don't, because you forgot to ask the question.
Reply:It needs to be in the InputData function:
cout%26lt;%26lt;"Enter cost of purchase : ";
cin%26gt;%26gt; CostOfPurchase;
RIGHT HERE
cout%26lt;%26lt;endl%26lt;%26lt;endl;
---------------------------------
Put an if statement RIGHT HERE, checking the value of CostOfPurchase. If it equals -1, then exit the program.
Reply:i use the exit(); command to just end the programs, however im using the customized compiler of C++ so im not sure if yours is the same... it shold be. in the while loop add if(answer == -1) exit();
Reply:I answered this question a few minutes ago, reposting is frowned upon, you should have editted your first question. I will put my answer here along with the additional question you have to answer.
You need to enter it in InputData() just before the first line. Also to break out of the void function without entering Amount Tendered, you need a return statement. So it should look like this:
void InputData()
{
while (CostOfPurchase != -1)
{
cout%26lt;%26lt;"Enter cost of purchase : ";
cin%26gt;%26gt; CostOfPurchase;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
}
return;
}
You will have to create a separate function for getting Amount Tendered or else it will never reach there.
At least that's what it seems like your asking
Bet of Luck!
Reply:so umm what's your question exactly?
int main ( )
{
while(CostOfPurchase != -1)
{
InputData();
}
return 0;
}
void InputData()
{
cout%26lt;%26lt;"Enter cost of purchase : ";
cin%26gt;%26gt; CostOfPurchase;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
if(CostOfPurchase!=-1)
{
cout%26lt;%26lt;"Enter amount tendered : ";
cin%26gt;%26gt; AmountTendered;
cout%26lt;%26lt;endl%26lt;%26lt;endl;
CHANGE();
OutputData();
}
}
PS I'm assuming CostOfPurchase is a "global" variable, bad practice btw.
Reply:Maybe this:
int main ( )
{
InputData();
while(CostOfPurchase != -1)
{
CHANGE();
OutputData();
InputData(); //I just added this
}
return 0;
}
Friday, July 31, 2009
C++ Programming Trouble?
I started the problem but I am stock. I do not know what to do. Please help!
Write a program that asks for two positive fractions, displays the fractions, and returns both the exact sum and
the exact product of the fractions in lowest terms.
To display a fraction “a / b”, where a and b are the numerator and the denominator, you may use a cout statement
that uses ...%26lt;%26lt; a %26lt;%26lt;" / " %26lt;%26lt; b %26lt;%26lt; ...
#include %26lt;iostream%26gt;
using namespace std;
struct frac{int numer, denom;};
frac f_add( frac x, frac y );
int main( ){
frac r, s, sum;
cout %26lt;%26lt; "Enter the numerator and the denominator of the first positive fraction separated by a space=%26gt; " %26lt;%26lt; endl;
cin %26gt;%26gt; r.numer %26gt;%26gt; r.denom;
cout %26lt;%26lt; "Enter the numerator and the denominator of the second positive fraction separate by a space=%26gt; " %26lt;%26lt; endl;
cin %26gt;%26gt; s.numer %26gt;%26gt; s.denom;
sum = f_add(r,s);
cout %26lt;%26lt; "The sum of " %26lt;%26lt; r.numer %26lt;%26lt;"/" %26lt;%26lt; r.denom %26lt;%26lt; " and "%26lt;%26lt; s.numer %26lt;%26lt;"/" %26lt;%26lt; s.denom %26lt;%26lt;" in lowest term is " %26lt;%26lt; sum.numer %26lt;%26lt;"/" %26lt;%26lt; sum.denom %26lt;%26lt;".\n";
system ("Pause");
return 0;
}
frac f_add(frac x, frac y){
}
C++ Programming Trouble?
It seems you were given the template of the program, now you have to fill in the f_add function that takes two frac structures as input (x and y) and returns the sum.
Within f_add you have x.numer, x.denom and y.numer, y.denom. If you were to *multiply* x and y, them, you'd just do:
frac result;
result.numer = x.numer * y.numer;
result.denom = x.denom * y.denom;
return result;
Of course, you have to add them, which is just a bit harder (just remember how to add fractions), I'll let you do it hoping you get the idea.
tropical flowers
Write a program that asks for two positive fractions, displays the fractions, and returns both the exact sum and
the exact product of the fractions in lowest terms.
To display a fraction “a / b”, where a and b are the numerator and the denominator, you may use a cout statement
that uses ...%26lt;%26lt; a %26lt;%26lt;" / " %26lt;%26lt; b %26lt;%26lt; ...
#include %26lt;iostream%26gt;
using namespace std;
struct frac{int numer, denom;};
frac f_add( frac x, frac y );
int main( ){
frac r, s, sum;
cout %26lt;%26lt; "Enter the numerator and the denominator of the first positive fraction separated by a space=%26gt; " %26lt;%26lt; endl;
cin %26gt;%26gt; r.numer %26gt;%26gt; r.denom;
cout %26lt;%26lt; "Enter the numerator and the denominator of the second positive fraction separate by a space=%26gt; " %26lt;%26lt; endl;
cin %26gt;%26gt; s.numer %26gt;%26gt; s.denom;
sum = f_add(r,s);
cout %26lt;%26lt; "The sum of " %26lt;%26lt; r.numer %26lt;%26lt;"/" %26lt;%26lt; r.denom %26lt;%26lt; " and "%26lt;%26lt; s.numer %26lt;%26lt;"/" %26lt;%26lt; s.denom %26lt;%26lt;" in lowest term is " %26lt;%26lt; sum.numer %26lt;%26lt;"/" %26lt;%26lt; sum.denom %26lt;%26lt;".\n";
system ("Pause");
return 0;
}
frac f_add(frac x, frac y){
}
C++ Programming Trouble?
It seems you were given the template of the program, now you have to fill in the f_add function that takes two frac structures as input (x and y) and returns the sum.
Within f_add you have x.numer, x.denom and y.numer, y.denom. If you were to *multiply* x and y, them, you'd just do:
frac result;
result.numer = x.numer * y.numer;
result.denom = x.denom * y.denom;
return result;
Of course, you have to add them, which is just a bit harder (just remember how to add fractions), I'll let you do it hoping you get the idea.
tropical flowers
C++ multiplying 2d 3x3 matrix arrays in a loop?
How do I multiply each element of the matrix, say by 5, to display in output code?
#include %26lt;iostream%26gt;
#include %26lt;math.h%26gt;
#include %26lt;string%26gt;
using namespace std;
char quit;
int main( )
{
//declaring the 2d matrix arrays
float matrixOne[3][3];
float matrixTwo[3][3];
cout %26lt;%26lt; "Week 5 Assignment, A-5B of two 2d 3x3 matrixes by DR. " %26lt;%26lt;endl;
//*************assigning numbers to the first matrix*********
for (int row = 0; row %26lt; 3; row++)
for (int column = 0; column %26lt; 3; column++)
{
cout %26lt;%26lt; "Enter value for row " %26lt;%26lt;row+1 %26lt;%26lt; " column " %26lt;%26lt;column+1 %26lt;%26lt; ": " %26lt;%26lt; endl;
cin %26gt;%26gt; matrixOne[row][column];
}
//************************************...
//calculation goes here ******
//***********
//*********outputting final matrix ***********
int row, column;
for (row = 0; row %26lt; 3; row++)
{
for (column = 0; column %26lt; 3; column++)
cout %26lt;%26lt; matrixOne[row][column];
cout %26lt;%26lt; endl;
}
//************************************...
cout %26lt;%26lt;"Type quit to exit " %26lt;%26lt; endl;
cin%26gt;%26gt;quit;
C++ multiplying 2d 3x3 matrix arrays in a loop?
well...I feel like i am doing your hw for you...but since i'm a bit rusty on my c++, I'm going to...pseudo this for you...the calculation section anyways. There are different ways you can do this, but off the top of my head, I thought of 2 similar ways
======================
1) take each input while its happening and multiply it right there before you put it in the matrix. example: after the cin%26gt;%26gt;matrixOne[row]column]; line, put something like matrixOne[row][column] = matrixOne[row][column] x 5;
=======================
2) if you need the actual calculations in the calculation section, you would need to do the whole nested loop again, [for (int row....) for (int column...) and in the nested loop, do the whole matrixOne[row][column] = matrixOne[row][column] x 5; line again.
=======================
And just a side note, it doesn't really really matter, but if you were to turn this program in as is, you might want to look at the following issues:
1) float matrixOne[3][3]; and float matrixTwo[3][3]; if you are putting in [3][3] in place of row and column, since row and column are both int, the declaration of float isn't necessary, because those values will never be a non whole number. Float takes up more memory in a program, and in bigger programs, you want to cut down on the memory space as much as possible.
2) If you are only going to use the matrixOne[3][3] and matrixTwo[3][3] for the row and column, you might want to consider coding it like this:
int row = 3;
int column = 3;
float matrixOne[row][column];
float matrixTwo[row][column];
that way, if you have to change all the number of rows in the whole program (changing your matrix size), you wouldn't have to go through each line that says float matrixOne[3][3]; and change them, you could just change the line that says int row = 3;.
3) putting float matrix[3][3] is declaring 4 spaces in memory for each row and column (you have 0, 1, 2, 3) and in the program, you only fill up 3 of those spaces with value. - for (int row = 0; row %26lt; 3; row++) notice the row %26lt; 3, and not row %26lt;=3.
4) instead of putting int row and int column in your for loop, and then later in your output, you should just put int once on top, like a global declaration. Besides, wont this give you an error like declaring it twice? I dont remember. It will help later one when you learn about functions and classes.
finally
5) cout type quit to exit, and cin quit. quit is a char, meaning it just takes one letter. so if you type quit, anything inputted after the first character is pointless. If you want to use char for quit, you can just tell the user to type q to exit.
Good luck with your programming!
Reply:hello friend press F7(function key) to trace the path of the program!
#include %26lt;iostream%26gt;
#include %26lt;math.h%26gt;
#include %26lt;string%26gt;
using namespace std;
char quit;
int main( )
{
//declaring the 2d matrix arrays
float matrixOne[3][3];
float matrixTwo[3][3];
cout %26lt;%26lt; "Week 5 Assignment, A-5B of two 2d 3x3 matrixes by DR. " %26lt;%26lt;endl;
//*************assigning numbers to the first matrix*********
for (int row = 0; row %26lt; 3; row++)
for (int column = 0; column %26lt; 3; column++)
{
cout %26lt;%26lt; "Enter value for row " %26lt;%26lt;row+1 %26lt;%26lt; " column " %26lt;%26lt;column+1 %26lt;%26lt; ": " %26lt;%26lt; endl;
cin %26gt;%26gt; matrixOne[row][column];
}
//************************************...
//calculation goes here ******
//***********
//*********outputting final matrix ***********
int row, column;
for (row = 0; row %26lt; 3; row++)
{
for (column = 0; column %26lt; 3; column++)
cout %26lt;%26lt; matrixOne[row][column];
cout %26lt;%26lt; endl;
}
//************************************...
cout %26lt;%26lt;"Type quit to exit " %26lt;%26lt; endl;
cin%26gt;%26gt;quit;
C++ multiplying 2d 3x3 matrix arrays in a loop?
well...I feel like i am doing your hw for you...but since i'm a bit rusty on my c++, I'm going to...pseudo this for you...the calculation section anyways. There are different ways you can do this, but off the top of my head, I thought of 2 similar ways
======================
1) take each input while its happening and multiply it right there before you put it in the matrix. example: after the cin%26gt;%26gt;matrixOne[row]column]; line, put something like matrixOne[row][column] = matrixOne[row][column] x 5;
=======================
2) if you need the actual calculations in the calculation section, you would need to do the whole nested loop again, [for (int row....) for (int column...) and in the nested loop, do the whole matrixOne[row][column] = matrixOne[row][column] x 5; line again.
=======================
And just a side note, it doesn't really really matter, but if you were to turn this program in as is, you might want to look at the following issues:
1) float matrixOne[3][3]; and float matrixTwo[3][3]; if you are putting in [3][3] in place of row and column, since row and column are both int, the declaration of float isn't necessary, because those values will never be a non whole number. Float takes up more memory in a program, and in bigger programs, you want to cut down on the memory space as much as possible.
2) If you are only going to use the matrixOne[3][3] and matrixTwo[3][3] for the row and column, you might want to consider coding it like this:
int row = 3;
int column = 3;
float matrixOne[row][column];
float matrixTwo[row][column];
that way, if you have to change all the number of rows in the whole program (changing your matrix size), you wouldn't have to go through each line that says float matrixOne[3][3]; and change them, you could just change the line that says int row = 3;.
3) putting float matrix[3][3] is declaring 4 spaces in memory for each row and column (you have 0, 1, 2, 3) and in the program, you only fill up 3 of those spaces with value. - for (int row = 0; row %26lt; 3; row++) notice the row %26lt; 3, and not row %26lt;=3.
4) instead of putting int row and int column in your for loop, and then later in your output, you should just put int once on top, like a global declaration. Besides, wont this give you an error like declaring it twice? I dont remember. It will help later one when you learn about functions and classes.
finally
5) cout type quit to exit, and cin quit. quit is a char, meaning it just takes one letter. so if you type quit, anything inputted after the first character is pointless. If you want to use char for quit, you can just tell the user to type q to exit.
Good luck with your programming!
Reply:hello friend press F7(function key) to trace the path of the program!
Subscribe to:
Posts (Atom)