Hmm I most likely am making this too complicated than needed...
Alright I got a little confused about the whole put assignment statements inside the if block... are the assignment statements pretty much the one in front of the for block?
Also taking the output statements outside of the loop... were you referring to the Total statement?
[PHP]#include <iostream.h>
#include <stdlib.h>
int main()
{
int i, x, total, sumPos, sumNeg;
for (int i = 0; i < 10; i++);
{
cout << "Enter 10 whole numbers";
cin >> x;
if (x <= 0) sumNeg+=x;
cout << "Sum of all numbers greater than 0: " << sumNeg << endl;
else sumPos+=x;
cout << "Sum of all numbers less than 0: " << sumPos << endl;
}
total = sumPos + sumNeg;
cout << "Sum of all numbers: " << total;
system("PAUSE");
return 0;
}[/PHP]
Also.. does it make a different of which goes first x<=0 / x>0 by means that the first output is the sum of Positive numbers opposed to negative.
That doesn't matter, but your output statements should all be together. Now that you have pretty much done the whole thing on your own let me show you how little code you can get away with.
[PHP]#include <iostream.h>
#include <stdlib.h>
int main()
{
int x, sumPos, sumNeg;
for (int i = 0; i < 10; i++);
{
cout << "Enter integer "<<i+1<<" of 10: ";
cin >> x;
if (x<0) sumNeg+=x;
else sumPos+=x;
}
cout << "Sum of all numbers less than 0: " << sumPos << endl;
cout << "Sum of all numbers greater than 0: " << sumNeg << endl;
cout << "Sum of all numbers: " << sumNeg+sumPos;
Hmm that doesn't seem to work as when testing I get this...
[php]Enter 10 whole numbers: 1 2 3 4 5 6 7 8 9 10
Sum of all numbers less than 0: 65537
Sum of all numbers greater than 0: 2009394757
Sum of all numbers: 2009460294Press any key to continue . . .[/php]And I have to add 'i' to the 'int' for it to execute.
As for all the outputs paired like that.. I never knew you could do it that way..
Further note, for the Greater/less than statements, shouldn't they be switched? As 'sumPos' should be for the greater than, and 'sumNeg' for the less than. Also... <code style="white-space: nowrap;"><code>"Enter integer "<<i+1<<" of 10: "</code></code>What is the 'i+1' there for? And this still allows the User to input 10 Whole numbers (With spaces) right?
Not to sound rude.. I did copy/paste your code and it wouldn't execute so I added the variable 'i' to the 'int' list... and that worked but this is the reult I get:
'Enter integer 37879713 of 10: 8
Sum of all numbers less than 0: 65544
Sum of all numbers greater than 0: 2009394757
Sum of all numbers: 2009460301Press any key to continue . . .'
when it ask's to enter an integer there is already a number...
int main()
{
int x, i, sumPos, sumNeg;
sumPos=0;
sumNeg=0;
for (i=0; i < 10; i++)
{
cout << "Enter integer "<<i+1<<" of 10: ";
cin >> x;
if (x<0) sumNeg+=x;
else sumPos+=x;
}
cout << "Sum of all numbers less than 0: " << sumPos << endl;
cout << "Sum of all numbers greater than 0: " << sumNeg << endl;
cout << "Sum of all numbers: " << sumNeg+sumPos;
system("PAUSE");
return 0;
} [/PHP]
ok, that's probably because I'm used to java programming. Looks like you don't declare the lcv inside the condition statement. it should work now. compile that and see where it gets you. I found another problem too, you had
[php]for (i=0; i < 10; i++);[/php]
you should have had
[php]for (i=0; i < 10; i++)[/php]
which may have been partially my mistake too for not even catching that.
The problem with the values is that variables are not initialized to 0.
So when you have
[php]
int sumPos;
[/php]
then sumPos is whatever value is already stored in that area of memory, it is NOT initialized to 0. To do that, use
[php]
int sumPos = 0;
[/php]
Now, you're adding values to 0, rather than adding values to some random value.
0 + 7 + 2 + 1 = 10 // expected
9181 + 7 + 2 + 1 = 9191 // probably not what you expect, as 9181 was supposed to be 0
The problem with the values is that variables are not initialized to 0.
So when you have
[php]
int sumPos;
[/php]
then sumPos is whatever value is already stored in that area of memory, it is NOT initialized to 0. To do that, use
[php]
int sumPos = 0;
[/php]
Now, you're adding values to 0, rather than adding values to some random value.
0 + 7 + 2 + 1 = 10 // expected
9181 + 7 + 2 + 1 = 9191 // probably not what you expect, as 9181 was supposed to be 0
Well, I feel stupid now. Don't know how I overlooked that one.:sad2: To think that I will be spending a lot of time programming from now on too as I'm majoring in computer engineering.
How would you change it to make it so you can just either input all numbers onto one line (With spaces) / put them with Enter but not have the message appear over and over again?
I'm not sure why you would want to, but if you only wanted the first input message to display once then take it outside and above the for loop. and get rid of the i+1 of 10, just say enter integers or something like that.
I'm not sure how you would do them all on one line in C++ I know there's a way to do it if I remember correctly, but not off the top of my head.
Q. 1: Write a program to score the paper-rock-scissor game. Each of two users types in either P,R, or S. The program then announces the winner as well as the basis for determining the winner: Paper covers rock, Rock breaks scissors, Scissors cut paper, or Nobody wins. Be sure to allow the users to use lowercase as well as uppercase letters. Your program should include a loop that lets the user play again until the user says she or he is done.
[php]#include
#include
int main()
{
int P, R, S;
char ans;
do
{
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans;
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans;
switch (ans)
{
case 'P':
case 'p':
cout << "Paper wins! Paper covers rock. ";
break;
case 'R':
case 'r':
cout << "Rock wins! Rock breaks scissors. ";
break;
case 'S':
case 's':
cout << "Scissors wins! Scissors cuts paper. ";
break;
cout << "\nWould you like to play again? (y or n): ";
cin >> ans;
}
}while (ans == 'Y' || ans == 'y');
system("PAUSE");
return 0;
}[/php]I am pretty sure that is the General layout of the coding but then I am unsure how to make it work work as the examples in the book are all number related opposed to using words.. and I don't know how to phrase teh Tie Game part.
Currently working on the second question which I will post up as I get a code in the making.
10. Write an astrology program. The user types in a birthday, and the program responds with the sign and horoscope for that birthday. The month may be entered as a number from 1 to 12. Then enhance your program so that if the birthday is only one or two days away from an adjacent sign, the program announces that the birthday is on a "cusp" and also outputs the horoscope for the nearest adjacent sign. This program will have a long multiway branch. Make up a horoscope for each sign. Your program should include a loop that lets the user repeat this calculation until the user says she or he is done.
[php]
__________________________________________________________________
Programmer : Christopher La
Project : Ch. 3.3
Class : CS2
Due : 3/15/07
Description : Write an astrology program. Make up a horoscope
for each sign. Your program should include a loop
that lets the user repeat this calculation until
the user says she or he is done.
___________________________________________________________________
*/
#include <iostream.h>
#include <stdlib.h>
int main()
{
int mm, dd;
do
{
cout >> "Enter your birthday (mm dd): \n";
cin << mm dd;
enum Horoscope_Signs
{
Aries March 21 - April 19
Taurus April 20 - May 20
Gemini May 21 - June 21
Cancer June 22 - July 22
Leo July 23 - August 22
Virgo August 23 - Suptember 22
Libra September 23 - October 22
Scorpio October 23 - November 21
Sagittariius November 22 - December 21
Capricorn December 22 - January 10
Aquarius January 20 - Februarry 19
Pisces February 10 - March 20
}
cout << "You will find be traveling to faraway places. "
cout << "Your communication skills will benefit you greatly. "
cout << "You will be attracting many people with your charms. "
cout << "Your generous heart shall attract many people. "
cout << "You will be open to many tasks, but don't overload yourself. "
cout << "You shall do what you can to maintain all your relationships. "
cout << "Your creative writing will get you very far. "
cout << "Your creative insight gains you recognition. "
cout << "People will come to you as you are a great listener. "
cout << "You ignore what doesn't interest you. "
cout << "You will find your destined soulmate. "
cout << "You will find your destined soulmate. "
cout << "\nTry again? (y or n): ";
cin >> ans;
}
while (ans == 'Y' || ans == 'y');
system("PAUSE");
return 0;
}[/php]
I know that is wrong but that is just listing the given information.. I am not too sure which statements to input for the program to generate it...
Man this sucks how not much of this information is sinking in.
</stdlib.h></iostream.h>
[php]
switch (ans)
{
case 'P':
case 'p':
cout << "Paper wins! Paper covers rock. ";
break;
case 'R':
case 'r':
cout << "Rock wins! Rock breaks scissors. ";
break;
case 'S':
case 's':
cout << "Scissors wins! Scissors cuts paper. ";
break;
cout << "\nWould you like to play again? (y or n): ";
cin >> ans;
}
}while (ans == 'Y' || ans == 'y');
[/php]
First, you're switching on ans, but it will only contain the second answer so you have no way to compare answers, therefore if ans is P, you cannot be certain that "Paper wins! Paper covers rock."
Second, you need to include the statements you want done in case a P/R/S is not sent under "default:":
[php]
switch (variable)
{
case A:
cout << "You typed A";
break;
case B:
cout << "You typed B";
break;
case C:
cout << "You typed C";
break;
default:
cout << "You did not type A, nor did you type B nor did you type C.";
break;
}
[/php]
Hmm so for the comparison of 2 answers I would still need to use the Switch statement but just enter the 2 if statements above the breaks... or just use the if statements on their own without using switch?
EDIT: Actually for those If statements... I'd have to write one out for every possible combination huh? Meaning I'd have to repeat it again after doing all the possible combinations with ans1 then again with ans2?
Well you could just do a precheck in the beginning that says
[PHP]
if (!(ans1==ans2)){
all your code here
}
else
cout << "Draw";
[/PHP]
thats really rough, but what I would do. And he could use a switch if he wanted to. He would just need three switches p, r, and s. then an if else statement inside each case.
so something like
[php]
if(ans!=ans2){
switch (ans)
{
case p:
if (ans2=='r')
cout<<"yadda yadda";
else
cout<<"yadda yadda";
break;
case r:
if (ans2=='s')
cout<<"yadda yadda";
else
cout<<"yadda yadda";
break;
case s:
if (ans2=='r')
cout<<"yadda yadda";
else
cout<<"yadda yadda";
break;
default:
cout << "There was a problem with your input, please try again";
break;
}
}
else
cout<<"draw";[/php]
Would that be the same as
[php]if (ans1 == 'P'|| ans1 == 'P') && (ans2 == 'r' || ans2 == 'R')[/php]
</span></span></code></code>
yeh, except I don't know why you would check if ans1 is P or P, perhaps you meant p or P. Though that will be pretty inefficient code. Because I think that C++ short circuits. I'm not sure though.
Hmmm quick question after seeing your previous code...
[php]/*
__________________________________________________________________
Programmer : Christopher La
Project : Ch. 3.1
Class : CS2
Due : 3/15/07
Description : Write a program to score the paper-rock-scissor
game. Be sure to allow the users to use lower-case
as well as uppercase letters. Your program should
include a loop that lets the user play again until
the user says she or he is done.
___________________________________________________________________
*/
#include <iostream.h>
#include <stdlib.h>
int main()
{
int p, r, s, variable;
char ans, ans1, ans2;
do
{
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans1;
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans2;
if(ans!=ans2)
{
switch (ans)
{
case 'p':
if (ans2=='r')
cout << "Paper wins! Paper covers rock. " << endl;
break;
case 'r':
if (ans2=='s')
cout << "Rock wins! Rock breaks scissors. " << endl;
break;
case 's':
if (ans2=='p')
cout << "Scissors wins! Scissors cuts paper. " << endl;
break;
default:
cout << "There was a problem with your input, please try again. ";
break;
}
}
else
cout<<"Tie game!" << endl;
cout << "Would you like to play again? (y or n): " << endl;
cin >> ans;
}
}while (ans == 'Y' || ans == 'y');
system("PAUSE");
return 0;
} [/php]
Could something like that work? As I am only showing who won so I am not sure what to fill in the 'else' part with...
Otherwise with a little playing around... I got this:
[php]/*
__________________________________________________________________
Programmer : Christopher La
Project : Ch. 3.1
Class : CS2
Due : 3/15/07
Description : Write a program to score the paper-rock-scissor
game. Be sure to allow the users to use lower-case
as well as uppercase letters. Your program should
include a loop that lets the user play again until
the user says she or he is done.
___________________________________________________________________
*/
#include <iostream.h>
#include <stdlib.h>
int main()
{
int p, r, s, variable;
char ans, ans1, ans2;
do
{
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans1;
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans2;
if(ans!=ans2)
{
switch (ans)
{
case 'p':
if (ans2=='r')
cout << "Paper wins! Paper covers rock. " << endl;
else
cout<<"yadda yadda";
break;
case 'r':
if (ans2=='s')
cout << "Rock wins! Rock breaks scissors. " << endl;
else
cout<<"yadda yadda";
break;
case 's':
if (ans2=='p')
cout << "Scissors wins! Scissors cuts paper. " << endl;
else
cout<<"yadda yadda";
break;
default:
cout << "There was a problem with your input, please try again. " << endl;
break;
}
}
else
cout<<"Tie game!" << endl;
cout << "Would you like to play again? (y or n): ";
cin >> ans;
}while (ans == 'Y' || ans == 'y');
system("PAUSE");
return 0;
}
[/php]
But when I run it.. always getting the wrong input message...
yeh, that first one is good, but you axed the else which you need. Its all about logic.
Ok, first you check to make sure that it isn't going to be a tie game. So with that done you know if ans1 is p, you know that ans2 has to be r or s. so thats why I check if ans2 equals r. if it is then paper wins, else scissors wins.
Do you see my logic?
If ans1 is p, then ans2 has to be s or r, if it isn't r then it has to be s.
If ans1 is r, then ans2 has to be s or p, if it isn't s then it has to be p.
If ans1 is s, then ans2 has to be p or r, if it isn't p then it has to be r .
Conversely:
If ans1 != ans2 then there is going to be a winner, else its a tie game.
if p is ans1, and s is ans2, then s wins, else p wins(beats rock)
if r is ans1, and p is ans2, then p wins, else r wins(beats scissors)
if s is ans1, and r is ans2, then r wins, else s wins(beats paper)
Pretty simple logic. To me that is the most elegant solution to the problem, though there is always a better way to do something.
You might also make some code to do some reassigning.
and so on. and do that for both vars. because I don't think there is a way for a case to accept s or S. you could go with if statements on this instead of the switch.
OK. No, that won't work. You're comparing ans to ans2, but you're wanting to compare ans1 to ans2. Also, you're switching on ans, and you should switch on ans1. You need to pay much closer attention to your code.
Let's take your code and play around with it some.
[php]
#include <iostream.h>
#include <stdlib.h>
int main()
{
char answer1, answer2, playagain;
do
{
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> answer1;
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> answer2;
if (answer1 != answer2)
{
switch (answer1)
{
case 'p':
case 'P':
if (answer2 == 'r' || answer2 == 'R')
cout << "Paper wins! Paper covers rock. " << endl;
break;
case 'r':
case 'R':
if (answer2 == 's' || answer2 == 'S')
cout << "Rock wins! Rock breaks scissors. " << endl;
break;
case 's':
case 'S':
if (answer2 == 'p' || answer2 == 'P')
cout << "Scissors wins! Scissors cuts paper. " << endl;
break;
default:
cout << "Incorrect selection, please try again. ";
break;
}
}
else
cout<<"Tie game!" << endl;
cout << "Would you like to play again? (Y or N): " << endl;
cin >> playagain;
} while (playagain == 'Y' || playagain == 'y');
system("PAUSE");
return 0;
}
[/php]
But, let me ask you this: What if answer1 == 'P' and answer2 == 'S', then that happens? You need to figure that out and see if there needs to be any adjustments to your code.
[php]/*
__________________________________________________________________
Programmer : Christopher La
Project : Ch. 3.1
Class : CS2
Due : 3/15/07
Description : Write a program to score the paper-rock-scissor
game. Be sure to allow the users to use lower-case
as well as uppercase letters. Your program should
include a loop that lets the user play again until
the user says she or he is done.
___________________________________________________________________
*/
#include <iostream.h>
#include <stdlib.h>
int main()
{
int p, r, s, variable;
char ans, ans1, ans2;
do
{
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans1;
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans2;
if(ans1 != ans2)
{
switch (ans1)
{
case 'p':
case 'P':
if (ans2 == 'r' || ans2 == 'R')
cout << "Paper wins! Paper covers rock. " << endl;
else
cout << "Scissors wins! Scissors cuts paper. " << endl;
break;
case 'r':
case 'R':
if (ans2 == 's' || ans2 == 'S')
cout << "Rock wins! Rock breaks scissors. " << endl;
else
cout << "Scissors wins! Scissors cuts paper. " << endl;
break;
case 's':
case 'S':
if (ans2 == 'p' || ans2 == 'P')
cout << "Scissors wins! Scissors cuts paper. " << endl;
else
cout << "Rock wins! Rock breaks scissors. " << endl;
break;
default:
cout << "There was a problem with your input, please try again. " << endl;
break;
}
}
else
cout<<"Tie game!" << endl;
cout << "Would you like to play again? (y or n): ";
cin >> ans;
}while (ans == 'Y' || ans == 'y');
system("PAUSE");
return 0;
}
[/php]
This works BUT the Tie Games aren't working right...
Here is a sample:
Enter Paper, Rock, or Scissors (P, R, S): S
Enter Paper, Rock, or Scissors (P, R, S): s
Rock wins! Rock breaks scissors.
Would you like to play again? (y or n): y
Enter Paper, Rock, or Scissors (P, R, S): P
Enter Paper, Rock, or Scissors (P, R, S): p
Scissors wins! Scissors cuts paper.
Would you like to play again? (y or n): y
Enter Paper, Rock, or Scissors (P, R, S): R
Enter Paper, Rock, or Scissors (P, R, S): r
Scissors wins! Scissors cuts paper.
Would you like to play again? (y or n): y
Enter Paper, Rock, or Scissors (P, R, S): R
Enter Paper, Rock, or Scissors (P, R, S): p
Scissors wins! Scissors cuts paper.
Would you like to play again? (y or n): y
Enter Paper, Rock, or Scissors (P, R, S): r
Enter Paper, Rock, or Scissors (P, R, S): r
Tie game!
Would you like to play again? (y or n): y
Enter Paper, Rock, or Scissors (P, R, S): S
Enter Paper, Rock, or Scissors (P, R, S): P
Scissors wins! Scissors cuts paper.
Would you like to play again? (y or n): n
Press any key to continue . . .
So yeah.. not too sure.. and for the 'Case' you cna just add the alternative "Upper/Lower' as I am reading that in the book but I don't know if that applies to the Input as you can see from the sample doesn't seem to work.
okay.. I played a little more by just guessing and got this:
[php]/*
__________________________________________________________________
Programmer : Christopher La
Project : Ch. 3.1
Class : CS2
Due : 3/15/07
Description : Write a program to score the paper-rock-scissor
game. Be sure to allow the users to use lower-case
as well as uppercase letters. Your program should
include a loop that lets the user play again until
the user says she or he is done.
___________________________________________________________________
*/
#include <iostream.h>
#include <stdlib.h>
int main()
{
char ans, ans1, ans2;
do
{
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans1;
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans2;
if(ans1 != ans2)
{
switch (ans1)
{
case 'p':
case 'P':
if (ans2 == 'r' || ans2 == 'R')
cout << "Paper wins! Paper covers rock. " << endl;
else if (ans2 == 's' || ans2 == 'S')
cout << "Scissors wins! Scissors cuts paper. " << endl;
break;
case 'r':
case 'R':
if (ans2 == 's' || ans2 == 'S')
cout << "Rock wins! Rock breaks scissors. " << endl;
else if (ans2 == 'p' || ans2 == 'P')
cout << "Scissors wins! Scissors cuts paper. " << endl;
break;
case 's':
case 'S':
if (ans2 == 'p' || ans2 == 'P')
cout << "Scissors wins! Scissors cuts paper. " << endl;
else if (ans2 == 'r' || ans2 == 'R')
cout << "Rock wins! Rock breaks scissors. " << endl;
break;
default:
cout << "There was a problem with your input, please try again. " << endl;
break;
}
}
else
cout<<"Tie game!" << endl;
cout << "Would you like to play again? (y or n): ";
cin >> ans;
}while (ans == 'Y' || ans == 'y');
system("PAUSE");
return 0;
}
[/php]
Works.. BUT now the problem is.. doesn't show tie game for alternating letters case.
Enter Paper, Rock, or Scissors (P, R, S): R
Enter Paper, Rock, or Scissors (P, R, S): R
Tie game!
Would you like to play again? (y or n): y
Enter Paper, Rock, or Scissors (P, R, S): R
Enter Paper, Rock, or Scissors (P, R, S): r
Would you like to play again? (y or n): y
Enter Paper, Rock, or Scissors (P, R, S): P
Enter Paper, Rock, or Scissors (P, R, S): s
Scissors wins! Scissors cuts paper.
Would you like to play again? (y or n): y
Enter Paper, Rock, or Scissors (P, R, S): p
Enter Paper, Rock, or Scissors (P, R, S): S
Scissors wins! Scissors cuts paper.
Would you like to play again? (y or n): n
Press any key to continue . . .
why not. all it does is take everything to uppercase.
I'm not familliar with C++ very much, but I suspect it will just be ans1.toUpper(); I doubt you have to instantiate an object since there wouldn't be much need to have more than one of those floating around.
Do you know why your tie games aren't working? its because if ans1 is uppercase and ans2 is lowercase then even if they are both r, they aren't the same. Thats why A2J said to use toUpper(), that way you can be sure you are only dealing with uppercase chars. Sorry about the ans/ans2. I don't usually call me lcv ans, so I just used it.
Think about it logically; you need both the answer and what you're comparing against to be in the same case at the time of comparison. However, you can't upper case the answer before it's been given, right? So that means you need to make sure they're the same case when?
Remember, structured programming code has a very logical order, but statements don't necessarily need to always be in one specific location in the code.
Programming is not like other school subjects. There's not always one right answer that you have to memorize. It's a way of thinking that you have to learn and become comfortable with. It's much better to learn the concepts and understand how things work than it is to just turn in an answer to the assignment...
How did you get the cin or the cout functionality? Where are they located?
However you were able to use those is the same way you'll be able to use toupper().
airbornflight: ans is not an object of the string class, therefore it has no .functions, it is nothing but a variable. Therefore it would be:
[php]
ans = toupper(ans);
[/php]
Comments
That doesn't matter, but your output statements should all be together. Now that you have pretty much done the whole thing on your own let me show you how little code you can get away with.
[PHP]#include <iostream.h>
#include <stdlib.h>
int main()
{
int x, sumPos, sumNeg;
for (int i = 0; i < 10; i++);
{
cout << "Enter integer "<<i+1<<" of 10: ";
cin >> x;
if (x<0) sumNeg+=x;
else sumPos+=x;
}
cout << "Sum of all numbers less than 0: " << sumPos << endl;
cout << "Sum of all numbers greater than 0: " << sumNeg << endl;
cout << "Sum of all numbers: " << sumNeg+sumPos;
system("PAUSE");
return 0;
} [/PHP]
[php]Enter 10 whole numbers: 1 2 3 4 5 6 7 8 9 10
Sum of all numbers less than 0: 65537
Sum of all numbers greater than 0: 2009394757
Sum of all numbers: 2009460294Press any key to continue . . .[/php]And I have to add 'i' to the 'int' for it to execute.
As for all the outputs paired like that.. I never knew you could do it that way..
Further note, for the Greater/less than statements, shouldn't they be switched? As 'sumPos' should be for the greater than, and 'sumNeg' for the less than. Also... <code style="white-space: nowrap;"><code>"Enter integer "<<i+1<<" of 10: " </code></code>What is the 'i+1' there for? And this still allows the User to input 10 Whole numbers (With spaces) right?
As a side note, if i remember correctly a whole number is a positive number. An integer can either be positive or negative.
'Enter integer 37879713 of 10: 8
Sum of all numbers less than 0: 65544
Sum of all numbers greater than 0: 2009394757
Sum of all numbers: 2009460301Press any key to continue . . .'
when it ask's to enter an integer there is already a number...
#include <iostream.h>
#include <stdlib.h>
int main()
{
int x, i, sumPos, sumNeg;
sumPos=0;
sumNeg=0;
for (i=0; i < 10; i++)
{
cout << "Enter integer "<<i+1<<" of 10: ";
cin >> x;
if (x<0) sumNeg+=x;
else sumPos+=x;
}
cout << "Sum of all numbers less than 0: " << sumPos << endl;
cout << "Sum of all numbers greater than 0: " << sumNeg << endl;
cout << "Sum of all numbers: " << sumNeg+sumPos;
system("PAUSE");
return 0;
} [/PHP]
ok, that's probably because I'm used to java programming. Looks like you don't declare the lcv inside the condition statement. it should work now. compile that and see where it gets you. I found another problem too, you had
[php]for (i=0; i < 10; i++);[/php]
you should have had
[php]for (i=0; i < 10; i++)[/php]
which may have been partially my mistake too for not even catching that.
So when you have
[php]
int sumPos;
[/php]
then sumPos is whatever value is already stored in that area of memory, it is NOT initialized to 0. To do that, use
[php]
int sumPos = 0;
[/php]
Now, you're adding values to 0, rather than adding values to some random value.
0 + 7 + 2 + 1 = 10 // expected
9181 + 7 + 2 + 1 = 9191 // probably not what you expect, as 9181 was supposed to be 0
Well, I feel stupid now. Don't know how I overlooked that one.:sad2: To think that I will be spending a lot of time programming from now on too as I'm majoring in computer engineering.
How would you change it to make it so you can just either input all numbers onto one line (With spaces) / put them with Enter but not have the message appear over and over again?
I'm not sure how you would do them all on one line in C++ I know there's a way to do it if I remember correctly, but not off the top of my head.
<iostream.h><stdlib.h></stdlib.h></iostream.h>
[php]#include
#include
int main()
{
int P, R, S;
char ans;
do
{
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans;
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans;
switch (ans)
{
case 'P':
case 'p':
cout << "Paper wins! Paper covers rock. ";
break;
case 'R':
case 'r':
cout << "Rock wins! Rock breaks scissors. ";
break;
case 'S':
case 's':
cout << "Scissors wins! Scissors cuts paper. ";
break;
cout << "\nWould you like to play again? (y or n): ";
cin >> ans;
}
}while (ans == 'Y' || ans == 'y');
system("PAUSE");
return 0;
}[/php]I am pretty sure that is the General layout of the coding but then I am unsure how to make it work work as the examples in the book are all number related opposed to using words.. and I don't know how to phrase teh Tie Game part.
Currently working on the second question which I will post up as I get a code in the making.
10. Write an astrology program. The user types in a birthday, and the program responds with the sign and horoscope for that birthday. The month may be entered as a number from 1 to 12. Then enhance your program so that if the birthday is only one or two days away from an adjacent sign, the program announces that the birthday is on a "cusp" and also outputs the horoscope for the nearest adjacent sign. This program will have a long multiway branch. Make up a horoscope for each sign. Your program should include a loop that lets the user repeat this calculation until the user says she or he is done.
[php]
__________________________________________________________________
Programmer : Christopher La
Project : Ch. 3.3
Class : CS2
Due : 3/15/07
Description : Write an astrology program. Make up a horoscope
for each sign. Your program should include a loop
that lets the user repeat this calculation until
the user says she or he is done.
___________________________________________________________________
*/
#include <iostream.h>
#include <stdlib.h>
int main()
{
int mm, dd;
do
{
cout >> "Enter your birthday (mm dd): \n";
cin << mm dd;
enum Horoscope_Signs
{
Aries March 21 - April 19
Taurus April 20 - May 20
Gemini May 21 - June 21
Cancer June 22 - July 22
Leo July 23 - August 22
Virgo August 23 - Suptember 22
Libra September 23 - October 22
Scorpio October 23 - November 21
Sagittariius November 22 - December 21
Capricorn December 22 - January 10
Aquarius January 20 - Februarry 19
Pisces February 10 - March 20
}
cout << "You will find be traveling to faraway places. "
cout << "Your communication skills will benefit you greatly. "
cout << "You will be attracting many people with your charms. "
cout << "Your generous heart shall attract many people. "
cout << "You will be open to many tasks, but don't overload yourself. "
cout << "You shall do what you can to maintain all your relationships. "
cout << "Your creative writing will get you very far. "
cout << "Your creative insight gains you recognition. "
cout << "People will come to you as you are a great listener. "
cout << "You ignore what doesn't interest you. "
cout << "You will find your destined soulmate. "
cout << "You will find your destined soulmate. "
cout << "\nTry again? (y or n): ";
cin >> ans;
}
while (ans == 'Y' || ans == 'y');
system("PAUSE");
return 0;
}[/php]
I know that is wrong but that is just listing the given information.. I am not too sure which statements to input for the program to generate it...
Man this sucks how not much of this information is sinking in.
</stdlib.h></iostream.h>
You're storing both answers in the same variable, therefore the second answer overwrites the first and you'll have no way of knowing both answers.
try:
[php]
cout << "Enter Paper, Rock or Scissors (P, R, S): ";
cin >> ans1;
cout << "Enter Paper, Rock or Scissors (P, R, S): ";
cin >> ans2;
[/php]
First, you're switching on ans, but it will only contain the second answer so you have no way to compare answers, therefore if ans is P, you cannot be certain that "Paper wins! Paper covers rock."
Second, you need to include the statements you want done in case a P/R/S is not sent under "default:":
[php]
switch (variable)
{
case A:
cout << "You typed A";
break;
case B:
cout << "You typed B";
break;
case C:
cout << "You typed C";
break;
default:
cout << "You did not type A, nor did you type B nor did you type C.";
break;
}
[/php]
You need to compare both answers, don't just switch on one.
[php]
if (ans1 == 'P' || ans1 == 'P') {
if (ans2 == 'p' || ans2 == 'P') {
cout << "No winner. Please try again.";
}
.
.
.
}
[/php]
EDIT: Actually for those If statements... I'd have to write one out for every possible combination huh? Meaning I'd have to repeat it again after doing all the possible combinations with ans1 then again with ans2?
EDIT2:
[php]#include <iostream.h>
#include <stdlib.h>
int main()
{
int P, R, S, variable;
char ans, ans1, ans2;
do
{
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans1;
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans2;
switch (variable)
{
case 'A':
case 'a':
if (ans1 == 'P' || ans1 == 'P')
{
if (ans2 == 'r' || ans2 == 'R')
cout << "Paper wins! Paper covers rock. " << endl;
}break;
case 'R':
case 'r':
if (ans1 == 's' || ans1 == 'S')
{
if (ans2 == 'r' || ans2 == 'R')
cout << "Rock wins! Rock breaks scissors. " << endl;
}break;
case 'S':
case 's':
cout << "Scissors wins! Scissors cuts paper. " << endl;
break;
default:
cout << "You inputted an invalid variable. " << endl;
break;
cout << "Would you like to play again? (y or n): ";
cin >> ans;
}
}while (ans == 'Y' || ans == 'y');
system("PAUSE");
return 0;
} [/php]
Something similar to that or no? {For case A} that is.</stdlib.h></iostream.h>
[PHP]
if (!(ans1==ans2)){
all your code here
}
else
cout << "Draw";
[/PHP]
thats really rough, but what I would do. And he could use a switch if he wanted to. He would just need three switches p, r, and s. then an if else statement inside each case.
so something like
[php]
if(ans!=ans2){
switch (ans)
{
case p:
if (ans2=='r')
cout<<"yadda yadda";
else
cout<<"yadda yadda";
break;
case r:
if (ans2=='s')
cout<<"yadda yadda";
else
cout<<"yadda yadda";
break;
case s:
if (ans2=='r')
cout<<"yadda yadda";
else
cout<<"yadda yadda";
break;
default:
cout << "There was a problem with your input, please try again";
break;
}
}
else
cout<<"draw";[/php]
[php]if (ans1 == 'P' || ans1 == 'P')
{
if (ans2 == 'r' || ans2 == 'R')[/php]
Would that be the same as
[php]if (ans1 == 'P'|| ans1 == 'P') && (ans2 == 'r' || ans2 == 'R')[/php]
</span></span></code></code>
yeh, except I don't know why you would check if ans1 is P or P, perhaps you meant p or P. Though that will be pretty inefficient code. Because I think that C++ short circuits. I'm not sure though.
[php]/*
__________________________________________________________________
Programmer : Christopher La
Project : Ch. 3.1
Class : CS2
Due : 3/15/07
Description : Write a program to score the paper-rock-scissor
game. Be sure to allow the users to use lower-case
as well as uppercase letters. Your program should
include a loop that lets the user play again until
the user says she or he is done.
___________________________________________________________________
*/
#include <iostream.h>
#include <stdlib.h>
int main()
{
int p, r, s, variable;
char ans, ans1, ans2;
do
{
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans1;
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans2;
if(ans!=ans2)
{
switch (ans)
{
case 'p':
if (ans2=='r')
cout << "Paper wins! Paper covers rock. " << endl;
break;
case 'r':
if (ans2=='s')
cout << "Rock wins! Rock breaks scissors. " << endl;
break;
case 's':
if (ans2=='p')
cout << "Scissors wins! Scissors cuts paper. " << endl;
break;
default:
cout << "There was a problem with your input, please try again. ";
break;
}
}
else
cout<<"Tie game!" << endl;
cout << "Would you like to play again? (y or n): " << endl;
cin >> ans;
}
}while (ans == 'Y' || ans == 'y');
system("PAUSE");
return 0;
} [/php]
Could something like that work? As I am only showing who won so I am not sure what to fill in the 'else' part with...
Otherwise with a little playing around... I got this:
[php]/*
__________________________________________________________________
Programmer : Christopher La
Project : Ch. 3.1
Class : CS2
Due : 3/15/07
Description : Write a program to score the paper-rock-scissor
game. Be sure to allow the users to use lower-case
as well as uppercase letters. Your program should
include a loop that lets the user play again until
the user says she or he is done.
___________________________________________________________________
*/
#include <iostream.h>
#include <stdlib.h>
int main()
{
int p, r, s, variable;
char ans, ans1, ans2;
do
{
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans1;
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans2;
if(ans!=ans2)
{
switch (ans)
{
case 'p':
if (ans2=='r')
cout << "Paper wins! Paper covers rock. " << endl;
else
cout<<"yadda yadda";
break;
case 'r':
if (ans2=='s')
cout << "Rock wins! Rock breaks scissors. " << endl;
else
cout<<"yadda yadda";
break;
case 's':
if (ans2=='p')
cout << "Scissors wins! Scissors cuts paper. " << endl;
else
cout<<"yadda yadda";
break;
default:
cout << "There was a problem with your input, please try again. " << endl;
break;
}
}
else
cout<<"Tie game!" << endl;
cout << "Would you like to play again? (y or n): ";
cin >> ans;
}while (ans == 'Y' || ans == 'y');
system("PAUSE");
return 0;
}
[/php]
But when I run it.. always getting the wrong input message...
Ok, first you check to make sure that it isn't going to be a tie game. So with that done you know if ans1 is p, you know that ans2 has to be r or s. so thats why I check if ans2 equals r. if it is then paper wins, else scissors wins.
Do you see my logic?
If ans1 is p, then ans2 has to be s or r, if it isn't r then it has to be s.
If ans1 is r, then ans2 has to be s or p, if it isn't s then it has to be p.
If ans1 is s, then ans2 has to be p or r, if it isn't p then it has to be r .
Conversely:
If ans1 != ans2 then there is going to be a winner, else its a tie game.
if p is ans1, and s is ans2, then s wins, else p wins(beats rock)
if r is ans1, and p is ans2, then p wins, else r wins(beats scissors)
if s is ans1, and r is ans2, then r wins, else s wins(beats paper)
Pretty simple logic. To me that is the most elegant solution to the problem, though there is always a better way to do something.
You might also make some code to do some reassigning.
if(ans1=='S')
ans1='s';
else if(ans1='P')
ans1='p';
and so on. and do that for both vars. because I don't think there is a way for a case to accept s or S. you could go with if statements on this instead of the switch.
OK. No, that won't work. You're comparing ans to ans2, but you're wanting to compare ans1 to ans2. Also, you're switching on ans, and you should switch on ans1. You need to pay much closer attention to your code.
[php]
#include <iostream.h>
#include <stdlib.h>
int main()
{
char answer1, answer2, playagain;
do
{
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> answer1;
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> answer2;
if (answer1 != answer2)
{
switch (answer1)
{
case 'p':
case 'P':
if (answer2 == 'r' || answer2 == 'R')
cout << "Paper wins! Paper covers rock. " << endl;
break;
case 'r':
case 'R':
if (answer2 == 's' || answer2 == 'S')
cout << "Rock wins! Rock breaks scissors. " << endl;
break;
case 's':
case 'S':
if (answer2 == 'p' || answer2 == 'P')
cout << "Scissors wins! Scissors cuts paper. " << endl;
break;
default:
cout << "Incorrect selection, please try again. ";
break;
}
}
else
cout<<"Tie game!" << endl;
cout << "Would you like to play again? (Y or N): " << endl;
cin >> playagain;
} while (playagain == 'Y' || playagain == 'y');
system("PAUSE");
return 0;
}
[/php]
But, let me ask you this: What if answer1 == 'P' and answer2 == 'S', then that happens? You need to figure that out and see if there needs to be any adjustments to your code.
[php]/*
__________________________________________________________________
Programmer : Christopher La
Project : Ch. 3.1
Class : CS2
Due : 3/15/07
Description : Write a program to score the paper-rock-scissor
game. Be sure to allow the users to use lower-case
as well as uppercase letters. Your program should
include a loop that lets the user play again until
the user says she or he is done.
___________________________________________________________________
*/
#include <iostream.h>
#include <stdlib.h>
int main()
{
int p, r, s, variable;
char ans, ans1, ans2;
do
{
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans1;
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans2;
if(ans1 != ans2)
{
switch (ans1)
{
case 'p':
case 'P':
if (ans2 == 'r' || ans2 == 'R')
cout << "Paper wins! Paper covers rock. " << endl;
else
cout << "Scissors wins! Scissors cuts paper. " << endl;
break;
case 'r':
case 'R':
if (ans2 == 's' || ans2 == 'S')
cout << "Rock wins! Rock breaks scissors. " << endl;
else
cout << "Scissors wins! Scissors cuts paper. " << endl;
break;
case 's':
case 'S':
if (ans2 == 'p' || ans2 == 'P')
cout << "Scissors wins! Scissors cuts paper. " << endl;
else
cout << "Rock wins! Rock breaks scissors. " << endl;
break;
default:
cout << "There was a problem with your input, please try again. " << endl;
break;
}
}
else
cout<<"Tie game!" << endl;
cout << "Would you like to play again? (y or n): ";
cin >> ans;
}while (ans == 'Y' || ans == 'y');
system("PAUSE");
return 0;
}
[/php]
This works BUT the Tie Games aren't working right...
Here is a sample:
Enter Paper, Rock, or Scissors (P, R, S): S
Enter Paper, Rock, or Scissors (P, R, S): s
Rock wins! Rock breaks scissors.
Would you like to play again? (y or n): y
Enter Paper, Rock, or Scissors (P, R, S): P
Enter Paper, Rock, or Scissors (P, R, S): p
Scissors wins! Scissors cuts paper.
Would you like to play again? (y or n): y
Enter Paper, Rock, or Scissors (P, R, S): R
Enter Paper, Rock, or Scissors (P, R, S): r
Scissors wins! Scissors cuts paper.
Would you like to play again? (y or n): y
Enter Paper, Rock, or Scissors (P, R, S): R
Enter Paper, Rock, or Scissors (P, R, S): p
Scissors wins! Scissors cuts paper.
Would you like to play again? (y or n): y
Enter Paper, Rock, or Scissors (P, R, S): r
Enter Paper, Rock, or Scissors (P, R, S): r
Tie game!
Would you like to play again? (y or n): y
Enter Paper, Rock, or Scissors (P, R, S): S
Enter Paper, Rock, or Scissors (P, R, S): P
Scissors wins! Scissors cuts paper.
Would you like to play again? (y or n): n
Press any key to continue . . .
So yeah.. not too sure.. and for the 'Case' you cna just add the alternative "Upper/Lower' as I am reading that in the book but I don't know if that applies to the Input as you can see from the sample doesn't seem to work.
The tie games fix is not difficult, but that's the least of the problems.
[php]/*
__________________________________________________________________
Programmer : Christopher La
Project : Ch. 3.1
Class : CS2
Due : 3/15/07
Description : Write a program to score the paper-rock-scissor
game. Be sure to allow the users to use lower-case
as well as uppercase letters. Your program should
include a loop that lets the user play again until
the user says she or he is done.
___________________________________________________________________
*/
#include <iostream.h>
#include <stdlib.h>
int main()
{
char ans, ans1, ans2;
do
{
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans1;
cout << "Enter Paper, Rock, or Scissors (P, R, S): ";
cin >> ans2;
if(ans1 != ans2)
{
switch (ans1)
{
case 'p':
case 'P':
if (ans2 == 'r' || ans2 == 'R')
cout << "Paper wins! Paper covers rock. " << endl;
else if (ans2 == 's' || ans2 == 'S')
cout << "Scissors wins! Scissors cuts paper. " << endl;
break;
case 'r':
case 'R':
if (ans2 == 's' || ans2 == 'S')
cout << "Rock wins! Rock breaks scissors. " << endl;
else if (ans2 == 'p' || ans2 == 'P')
cout << "Scissors wins! Scissors cuts paper. " << endl;
break;
case 's':
case 'S':
if (ans2 == 'p' || ans2 == 'P')
cout << "Scissors wins! Scissors cuts paper. " << endl;
else if (ans2 == 'r' || ans2 == 'R')
cout << "Rock wins! Rock breaks scissors. " << endl;
break;
default:
cout << "There was a problem with your input, please try again. " << endl;
break;
}
}
else
cout<<"Tie game!" << endl;
cout << "Would you like to play again? (y or n): ";
cin >> ans;
}while (ans == 'Y' || ans == 'y');
system("PAUSE");
return 0;
}
[/php]
Works.. BUT now the problem is.. doesn't show tie game for alternating letters case.
Enter Paper, Rock, or Scissors (P, R, S): R
Enter Paper, Rock, or Scissors (P, R, S): R
Tie game!
Would you like to play again? (y or n): y
Enter Paper, Rock, or Scissors (P, R, S): R
Enter Paper, Rock, or Scissors (P, R, S): r
Would you like to play again? (y or n): y
Enter Paper, Rock, or Scissors (P, R, S): P
Enter Paper, Rock, or Scissors (P, R, S): s
Scissors wins! Scissors cuts paper.
Would you like to play again? (y or n): y
Enter Paper, Rock, or Scissors (P, R, S): p
Enter Paper, Rock, or Scissors (P, R, S): S
Scissors wins! Scissors cuts paper.
Would you like to play again? (y or n): n
Press any key to continue . . .
I'm not familliar with C++ very much, but I suspect it will just be ans1.toUpper(); I doubt you have to instantiate an object since there wouldn't be much need to have more than one of those floating around.
Do you know why your tie games aren't working? its because if ans1 is uppercase and ans2 is lowercase then even if they are both r, they aren't the same. Thats why A2J said to use toUpper(), that way you can be sure you are only dealing with uppercase chars. Sorry about the ans/ans2. I don't usually call me lcv ans, so I just used it.
Remember, structured programming code has a very logical order, but statements don't necessarily need to always be in one specific location in the code.
Programming is not like other school subjects. There's not always one right answer that you have to memorize. It's a way of thinking that you have to learn and become comfortable with. It's much better to learn the concepts and understand how things work than it is to just turn in an answer to the assignment...
However you were able to use those is the same way you'll be able to use toupper().
airbornflight: ans is not an object of the string class, therefore it has no .functions, it is nothing but a variable. Therefore it would be:
[php]
ans = toupper(ans);
[/php]