shipname[0] = "Aircraft";
shipname[1] = "Battleship";
shipname[2] = "Destoryer";
shipname[3] = "Submarine";
shipname[4] = "Patrol Boat";
cout << "Do you wish to place your own ships. Y/N ";
cin >> Isplaceship;
if ((Isplaceship = "Y") | (Isplaceship = "y"))
{
for (int i = 0; i < 5; i++){
cout << "Please Enter a location for your " << shipname[i] << endl;
cout << "type row. col & direction(0 horizontal, 1 vertical) split by spaces: ";
cin >> x >> y >> dir;
cout << "your input is " << x << " " << y << " " << dir;
}
cout << endl;
}
else if ((Isplaceship = "N") | (Isplaceship = "n"))
{
};
}
So I want to make a battleship game, but in this stage i want the user input their input according the the ship, but it never stop and just display all the result in the shipname. I never can enter any input.
I did some coding myself:
int i = 0;
for (int i = 0; i <= 5; ++i)
{
cout << "input a number:" << endl;
cin >> i;
cout << "The number you input is:" << i << endl;
}
And the result is:
input a number:
1
The number you input is:1
input a number:
2
The number you input is:2
input a number:
3
The number you input is:3
input a number:
4
The number you input is:4
input a number:
5
The number you input is:5
I am not so sure why you would get that result, but I think you can compare my code to yours or do some other similar coding, which may help you figure out some details that you miss.
Change this: if ((Isplaceship = "Y") | (Isplaceship = "y")) to this: if ((Isplaceship == "Y") | (Isplaceship == "y")).
Related
I am fairly new to C++, I am trying to write a code that allows input number of reviewers then allows number of reviewers to enter movie rating and display asterisks based on the number input. I am having difficulty incorporating an if statement that display "Movie ratings must be from 1 to 5." when the user input any number that's outside of 1 to 5. Another thing when it does work, it still continues the for loop of cout << "\nReviwer " << r << " rating: " ; instead of stopping and restarting. Any assistance is appreciated.
complied code
int reviewers;
int rating;
//input how many reviewers will be reviewing
cout << "How many reviewers? ";
cin >> reviewers;
cout << "Movie ratings must be from 1 to 5." << endl;
for (int r = 1; r <= reviewers; r++) {
cout << "\nReviwer " << r << " rating: ";
cin >> rating;
if (rating < 1 || rating > 5)
cout << "Movie ratings must be from 1 to 5." << endl;
else {
for (int j = 1; j <= rating; j++) {
cout << "* ";
} //end for
cout << endl;
} //end if
} //end for
Output example should be like this
You should have
#include <iostream>
#include <string>
using namespace std;
int main(){
int reviewers;
int rating;
//input how many reviewers will be reviewing
cout << "How many reviewers? ";
cin >> reviewers;
cout << "Movie ratings must be from 1 to 5." << endl;
for (int r = 1; r <= reviewers; r++) {
cout << "\nReviwer " << r << " rating: ";
cin >> rating;
while (rating < 1 || rating > 5){
cout << "Movie ratings must be from 1 to 5." << endl;
cout << "\nReviwer " << r << " rating: ";
cin >> rating;
}
if (rating >= 1 && rating <=5){
for (int j = 1; j <= rating; j++) {
cout << "* ";
} //end for
cout << endl;
} //end if
} //end for
}
};
It's not optimised but do the trick
if your mean is the reviewer must input the number between 1 and 5,
you can use a do while loop like this:
do
{
//enter rating;
}while (rating < 1||rating >5);
//print out rating;
I suppose you aim at something like this:
int reviewers;
int rating;
//input how many reviewers will be reviewing
cout << "How many reviewers? ";
cin >> reviewers;
cout << "Movie ratings must be from 1 to 5." << endl;
for (int r = 1; r <= reviewers; r++) {
while(true) {
cout << "\nReviewer " << r << " rating: ";
cin >> rating;
if (rating < 1 || rating > 5) {
cout << "Movie ratings must be from 1 to 5." << endl;
} else {
break;
}
}
for (int j = 1; j <= rating; j++) {
cout << "* ";
} //end for
cout << endl;
} //end for
Here is some code I quickly put together. I hope the comments are useful and if you have any questions just ask!
The main part is the recursive function get_ratings which will loop forever until it returns 1
// All this function does is returns the correct amount of stars
// E.G. make_stars(4) returns " * * * *"
string make_stars(int star_Count) {
string stars;
for (int i = 0 ; i < star_Count ; i++) {
stars += " *";
}
return stars;
}
// We get the ratings and returns 1 or 0 depending of it succeeded or failed
int get_ratings(int reviewer_count) {
// We initialise the ratings integer
int rating;
// We loop through all reviewers
for (int i = 0 ; i < reviewer_count ; i++) {
// We do i + 1 so it is more readable (usually in English we start at 1 not 0 like computers)
cout << "What is reviewer " << (i + 1) << "'s rating?" << endl;
//We get the user input
cin >> rating;
// We check to see if rating is withing the range. We could also do is NOT in the range and flip line 27 and 29
if (1 <= rating && rating <= 5) {
// If it is within range we will print the correct amount of stars
cout << make_stars(rating) << endl;
} else {
// We return 0 so we can determine the function "failed"
return 0;
}
}
// We return 1 so we can determine the function "succeeded"
return 1;
}
// This is a recursive function (it can run itself)
int get_ratings_rec(int reviewers) {
cout << "All ratings must be given between 1 and 5 (inclusive)" << endl;
// get_ratings_status is equal to 1/0 depending on if get_ratings() succeeded or failed
int get_ratings_status = get_ratings(reviewers);
if (get_ratings_status == 1) {
// If it was a success we print "Success!"
cout << "Success!" << endl;
} else {
// If it was a failure we tell the user and run get_ratings_loop() again until it succeeds
cout << "Failed, please try again\n" << endl;
get_ratings_loop(reviewers);
}
}
// Our main entry point to the program
int main() {
// We initialise the reviewers integer
int reviewers;
cout << "How many reviewers?\n>>> " << endl;
cin >> reviewers;
// We run get_ratings_loop() with the integer given
get_ratings_loop(reviewers);
}
You'd want to keep asking the number of stars if the user inputs a value outside the range, something like:
for (int r = 1; r <= reviewers; r++)
{
cout << "\nReviwer " << r << " rating: ";
do
{
cin >> rating;
if (rating < 1 || rating > 5) // print error message
cout << "Movie ratings must be from 1 to 5." << endl;
} while (rating < 1 || rating > 5); // repeat the loop if out of range
for (int j = 1; j <= rating; j++)
{
cout << "* ";
} //end for
cout << endl;
}
Note that you should be doing input validation also, e.g., if the input is an alphabetic character, your code will trigger an infinite loop, here an example of a possible solution:
#include <limits>
//...
do
{
if (!(cin >> rating))
{
std::cout << "Bad input, try again";
cin.clear(); //clear error flags
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // clear buffer
rating = 0;
}
if (rating < 1 || rating > 5)
cout << "Movie ratings must be from 1 to 5." << endl;
} while (rating < 1 || rating > 5);
I am a first year cs major. Today in our lab we had to debug some code and make it work. Below is the result.
#include <iostream>
using namespace std;
int main() {
int x = 3, y;
char myanswer;
int val= 1;
int num;
y = x;
cout << "y is set to: " << y << endl;
bool again = true;
int ans;
while (again) {
cout << "Please input a number: ";
cin >> y;
if (x > y)
cout << "X is greater than Y\n";
else {
cout << "X is less than Y" << endl;
cout << "would you like to input another number?" << endl;
cin >> ans;
if (ans != 1)
break;
}
cout << "would you like to input another number ?" << endl;
cin >> ans;
if (ans != 1)
again = false;
}
for (x = 0; x < 10; x++)
cout << x << endl;
cout << "What number would you like to find the factorial for? " << endl;
cin >> num;
cout << num;
for (int x = num; x > 0; x--) {
val *= x;
}
cout << "Are you enjoying cs161? (y or n) " << endl;
cin >> myanswer;
if (myanswer == 'y')
cout << "Yay!" << endl;
else
cout << "I hope you will soon!" << endl;
return 0;
}
After the cout regarding factorials, the cin's don't work and the user ceases to be able to enter input. So far my lab ta's and friends haven't been able to find the issue. The code has been compiled and exected on both my school's engineering servers and my local computer. On both the error persists.
almost certainly this caused an overflow
for (int x = num; x > 0; x--) {
val *= x;
}
what did you enter for num?
When you have a statement as:
cout << "would you like to input another number?" << endl;
The first instinct for the user would be to type y or n as an answer. You can help the user by providing a hint.
cout << "would you like to input another number (1 for yes, 0 for no)?" << endl;
If you do that, it would be better to be consistent throughout your program. The next prompt that seeks a y/n response must use the same mechanism.
cout << "Are you enjoying cs161? (1 for yes, 0 for no) " << endl;
Of course, always validate input operations before proceeding to use the data.
if ( !(cin >> ans) )
{
// Input failed. Add code to deal with the error.
}
So I've been reading a C++ Primer that has all kinds of examples and test scenarios inside that are good to make sure each new chapter has been correctly learnt and that there's no other way to improve that actually coding.
The C++ Primer asked for this "Practice Problem" to be attempted and is as follows:
Write a program that provides the option of tallying up the results of a poll with 3 possible values. The first input to the program is the poll question; the next three inputs are the possible answers. The first answer is indicated by 1, the second by 2, the third by 3. The answers are tallied until a 0 is entered. The program should then show the results of the poll—try making a bar graph that shows the results properly scaled to fit on your screen no matter how many results were entered.
So I was curious as to how the author wanted me to make sure that the results fit the screen resolution regardless of "how many results were entered" and after entering the main syntax for the loops that input the data and display. I was curious what the best way to go about this and slapped together a very, VERY simple work around that'll divide by a 1:10 ratio dependant on the highest result input (up to 1000/100)
#include <iostream>
#include <string>
using namespace std;
int main()
{
//Variable decleration
string Question;
int tally1 = 0, tally2 = 0, tally3 = 0;
int input = 1;
int resultRatio;
//Question input and text fluff
cout << "Please enter a poll that has 3 answers: ";
getline(cin, Question, '?');
cout << "Your question is: " << Question << "?" << endl;
cout << "When results are complete for specified poll answer, enter 0 to input results" << endl;
while (1) //Tally 1 answer input.
{
cout << "Please enter the tallies for answer A: ";
cin >> input;
tally1 = tally1 + input;
if (input != 0)
{
cout << "A's current tallies: " << tally1 << endl;
continue;
}
cout << endl;
break;
}
while (1) //Tally 2 answer input.
{
cout << "Please enter the tallies for answer B: ";
cin >> input;
tally2 = tally2 + input;
if (input != 0)
{
cout << "B's current tallies: " << tally2 << endl;
continue;
}
cout << endl;
break;
}
while (1) //Tally 3 answer input.
{
cout << "Please enter tallies for answer C: ";
cin >> input;
tally3 = tally3 + input;
if (input != 0)
{
cout << "C's current tallies: " << tally3 << endl;
continue;
}
cout << endl;
break;
}
// Ratio in which total tallies should be divded by before bar chart display
if (tally1 >= 10 || tally2 >= 10 || tally3 >= 10)
{
resultRatio = 10;
}
else if (tally1 >= 100 || tally2 >= 100 || tally3 >= 100)
{
resultRatio = 100;
}
else if (tally1 >= 1000 || tally2 >= 1000 || tally3 >= 1000)
{
resultRatio = 1000;
}
else
{
resultRatio = 1;
}
//Simple bar chart to display all the results in a ratio that'll fit the screen thanks to resultRatio division
cout << "All results have been entered, here is the barchart (Results displayed are divided by " << resultRatio <<"):";
cout << endl << "A:" << "(" << tally1 << "votes )";
for (int i = tally1 / resultRatio; i > 0; i--)
{
cout << "o";
}
cout << endl << "B:" << "(" << tally2 << "votes )";
for (int i = tally2 / resultRatio; i > 0; i--)
{
cout << "o";
}
cout << endl << "C:" << "(" << tally3 << "votes )";
for (int i = tally3 / resultRatio; i > 0; i--)
{
cout << "o";
}
cout << "\nHere is the full bar graph on input results";
return 0;
}
I am currently working on a sudoku game and I need help in adding a functionality that will make a value I input after selecting a row and column to flash red if wrong and green if correct
how do you make an input value flash green color when the right number is inputed by the user.
this is what my code look like for now.
if (randomize == 1)
{
for (int i = 0; i < 70; i++)
{
printGrid1();
cout << "Select row number: ";
cin >> row;
cout << endl;
cout << "Select column number: ";
cin >> col;
cout << endl;
cout << "Input Answer: ";
cin >> maybe;
cout << endl;
//Funtion to restart game
if ( col == 10|| row == 10 || answer ==10)
{
printTitle();
selectDifficulty();
}
//Check for out of bound cells
if (row > 9 ||col>9 ||row <1|| col<1)
{
cout << "This row or column is not in the Sudoku Board"<< endl;
}
answer = easyGuessNumGrid1Answer[row-1][col-1];
if (maybe != answer){
score = score - 5;
cout << "Your score is :" << score <<endl;
}
if (maybe == answer)
{
cout << "Good job! " << maybe << " was the right number. " << endl;
easyGuessNumGrid1[row-1][col-1] = answer;
i++;
}
else
{
cout << "incorrect, please try again..." << endl;
i--;
}
}}
I see a ton a questions for converting for loops to while and do while loops, but I cant seem to find anything on converting while loops to do while loops in C++. It still needs to maintain the same function as the original code as well.
Here is the original code:
int number, product = 1, count = 0;
cout << "Enter a whole number to be included in the product"
<< endl << "or enter 0 to end the input: ";
cin >> number;
while (number != 0)
{
product = product * number;
count++;
cout << "Enter a whole number to be included in the product"
<< endl << "or enter 0 to end the input: ";
cin >> number;
}
if (count > 0)
{
cout << endl << "The product is " << product << "." << endl;
}
By moving it around, I was able to end up here, but I keep ending up with errors. When I run the program, it prompts me to enter a while number, like expected. If I enter a valid number, it loops and asks me the same question. Then when I enter 0, it kicks out as usual, but it displays that the product is 0 no matter the numbers entered before.
Here is my attempt at adjusting it into a do while loop:
int main()
{
int number, product = 1, count = 0;
do
{
cout << "Enter a whole number to be included in the product" << endl << "or enter 0 to end the input: ";
cin >> number;
product = product * number;
count++;
}
while (number != 0);
{
if (count > 0)
cout << endl << "The product is " << product << "." << endl;
}
}
In the do .. while loop when you insert 0, first you multiply product by 0, than it exists. Therefore the product is always 0.
Move the product before:
int count = -1, number = 1, product = 1;
do
{
count++;
product = product * number; // you can use product *= number;
cout << "Enter a whole number to be included in the product" << endl << "or enter 0 to end the input: ";
cin >> number;
}
while (number != 0);
if (count > 0)
cout << endl << "The product is " << product << "." << endl;
Note: my code does not use additional if and still preserve the same functionality.
test number!=0 before assigning product = product*nuumber
otherwise, when user enters 0, you multiply by 0 and exit the loop
The problem is that in the do-while version, when the user inputs 0 this line:
product = product * number;
is executed, whereas in the while version not.
Because of this the product will always be 0.
If the number is 0 just don't multiply product.
Your problem was the number input on exit is 0, which makes your whole product equal to 0. Try this logic:
#include <iostream>
int main() {
int number = 0, product = 1, count = 0;
do {
std::cout << "Enter a whole number to be included in the product" << std::endl << "or enter 0 to end the input: ";
std::cin >> number;
if (number > 0) {
product = product * number;
count++;
}
} while (number != 0);
{
if (count > 0)
std::cout << std::endl << "The product is " << product << "." << std::endl;
}
}
If you just want to solve the problem that you are getting a 0 as your answer, then the problem is due to the following lines
cin >> number;
product = product * number;
you take the number which is 0 and multiply it with the product and obviously the result will be 0.
you can fix it by putting a break statement.
cin >> number;
if(number == 0)
break;
product = product * number;
However, in general I am not sure what you are achieving by trying to solve this problem i.e. converting a while to a do-while.
int number, product = 1, count = 0;
cout << "Enter a whole number to be included in the product"
<< endl << "or enter 0 to end the input: ";
cin >> number; // you enter a non-0 number here
while (number != 0) // you now loop until you hit 0 ...
{
product = product * number;
count++;
cout << "Enter a whole number to be included in the product"
<< endl << "or enter 0 to end the input: ";
cin >> number; // you overwrite the non-0 number you had previously input
}
if (count > 0)
{
cout << endl << "The product is " << product << "." << endl;
}
Without knowing the full context of your program, I'm guessing here, but you can probably rewrite it as:
int number = 0, product = 1, count = 0;
do
{
cout << "Enter a whole number to be included in the product"
<< endl << "or enter 0 to end the input: ";
cin >> number;
if (number != 0)
{
count++;
product *= number;
}
} while (number != 0); // exit the loop when you have a non-zero entry
if (count > 0)
{
cout << endl << "The product is " << product << "." << endl;
}
Or ...
int number = 0, product = 1;
do
{
cout << "Enter a whole number to be included in the product"
<< endl << "or enter 0 to end the input: ";
cin >> number;
if (number != 0)
{
product *= number;
}
} while (number != 0); // exit the loop when you have a non-zero entry
cout << endl << "The product is " << product << "." << endl;
You can avoid the 2nd condition since you are excluding 0 from potential inputs, and simply always output a product.
You are increasing count regardless of the input you get. First check if the input was zero (your abort input).
Change
count++
to
if (number)
count++;