Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm currently trying to work out some issues I am experiencing with this code, can't really figure out why I am getting these 2 errors.
I tried to see if something was not closed, but this not does seem to be the case, can be be cause of the distance between the ": "?
I'm just grasping for straws by now..
main.cpp:30:38: error: expected ‘;’ before ‘generationString’
cout << "Generation " << x << ": " generationString << endl;
main.cpp:54:40: error: expected ‘;’ before ‘generationString’
cout << "Generation " << x++ << ": " generationString << endl;
When trying to compile this code:
#include <iostream>
using namespace std;
string
initString ()
{
}
int
calculateScore (string guess, string target)
{
}
string
mutate (string mutationString)
{
}
int
main ()
{
string targetString = "METHINKS IT IS LIKE A WEASEL";
string generationString = initString ();
string currentString = generationString;
int score = calculateScore (currentString, targetString);
int x = 0;
cout << "Generation " << x << ": " generationString << endl;
do
{
for (int i = 0; i < 100; i++)
{
string newCopy = generationString;
newCopy = mutate (newCopy);
int copyScore = calculateScore (newCopy, targetString);
if (copyScore > score)
{
currentString = newCopy;
score = copyScore;
if (copyScore == targetString.length ())
{
break;
}
}
}
generationString = currentString;
}
while (score < targetString.length ());
cout << "Generation " << x++ << ": " generationString << endl;
return 0;
}
You're missing a <<.
cout << "Generation " << x << ": " generationString << endl;
Should be
cout << "Generation " << x << ": " << generationString << endl;
Likewise for
cout << "Generation " << x++ << ": " generationString << endl;
That should be
cout << "Generation " << x++ << ": " << generationString << endl;
You are probably missing a << in the line
cout << "Generation " << x << ": " generationString << endl;
Here you have
": " generationString
which should be
": " << generationString
C++ can concatenate literal strings, but it cannot concatenate literal string with anything else (like std::strings). So for instance this would work
cout << "Generation " << x << ": " "METHINKS IT IS LIKE A WEASEL" << endl;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
So I'm new to coding and I'm doing this for a class project. How do I make it where the while loop stops if it's <= to 0. If I put <= to 0 it doesn't do the loop at all and if I put it !=
it works, but it sometimes goes negative resulting in the loop never stopping. Is there something I'm missing? I have no idea why <= 0 wouldn't work
int main()
{
while (health != 0 && enemy_health != 0) {
std::cout << "Player " " Health: " << health << " Level: " << level << "\n";
std::cout << "Inventory " << "Gold: " << gold << " Keys: " << keys << " Health Potions: " << health_potions << "\n\n";
std::cout << "Fist: " << fist_damage << " damage " << " Torch: " << torch_damage << " damage \n" << " Status Effects:\n\n" <<" Burn: " << burn_damage << " damage\n\n";
std::cout << "Use: ";
std::cin >> input;
if (input == Fist) {
std::cout << "\nYou did " << fist_damage << " damage\n\n";
std::cout << "Monsters Health: " << enemy_health << "\n";
playerAttack = true;
}
else if (input == Torch) {
std::cout << "\nYou did " << torch_damage << " damage\n" << "You did " << burn_damage << " burn damage\n\n";
int total = enemy_health - (torch_damage + burn_damage);
enemy_health = total;
std::cout << "Monsters Health: " << enemy_health;
torch_durability--;
playerAttack = true;
isBurning = true;
}
while (playerAttack != true) {
std::cout << "\n\nPlease enter a valid answer\n\n";
std::cout << "Use: ";
std::cin >> input;
if (input == Fist) {
std::cout << "\nYou did " << fist_damage << " damage\n\n";
std::cout << "Monsters Health: " << enemy_health << "\n";
playerAttack = true;
}
else if (input == Torch) {
std::cout << "\nYou did " << torch_damage << " damage\n" << "You did " << burn_damage << " burn damage\n\n";
int total = enemy_health - (torch_damage + burn_damage);
enemy_health = total;
std::cout << "Monsters Health: " << enemy_health;
torch_durability--;
playerAttack = true;
isBurning = true;
}
}
if (playerAttack == true) {
std::cout << "\n\nThe enemy attacks\n\n";
}
if (dodge_chance == 5) {
std::cout << "\n\nYou dodged the enemies attack\n\n";
}
else if (dodge_chance != 5) {
std::cout << "The enemy landing a crushing blow\n\n";
playerHit = enemy1_damage;
int health_real = health - playerHit;
health = health_real;
std::cout << "The enemy does " << playerHit << " damage\n\n";
}
}
}
When asking questions or just for debugging, it is a good idea to isolate the part that is giving you trouble. Most of the code you have posted is irrelevant and makes it harder to read and test.
From what I understand, you have tried putting
while (health <= 0 && enemy_health <= 0)
instead of your current loop ?
If so, then it is simply a logic error. You are asking the code to loop while player health is negative or zero. Simply changing to
while (health > 0 && enemy_health > 0)
would work.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I have a simple program but it's running weirdly. Basically the code runs fine but when the numbering at the beginning of the line comes into play, int x++ displays the same number as the first line then continues. Why does this happen?
Code:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <string>
#include "logo.h"
int main()
{
SetConsoleTitle("plains.exe");
displayLogo();
int number;
int addTotal = 0;
int numbersEntered = 0;
std::cout << " [1] enter your first number: ";
std::cin >> number;
while (number != -1) {
addTotal = addTotal + number;
numbersEntered++;
std::cout << " [" << numbersEntered << "]" << " enter your next number or type '-1' to add them: ";
std::cin >> number;
}
if (number == -1) {
std::cout << " " << std::endl;
std::cout << " --------------------------------" << std::endl;
std::cout << " " << std::endl;
std::cout << " the sum of your numbers is " << addTotal << "." << std::endl;
std::cout << " you entered a total of " << numbersEntered << " numbers." << std::endl;
std::cout << " " << std::endl;
std::cout << " the average of your numbers is " << addTotal / numbersEntered << "." << std::endl;
std::cout << " " << std::endl;
}
return 0;
}
You initialized numbersEntered to 0. The first time through the while loop, it does numbersEntered++, which sets it to 1. So the first prompt in the loop contains [1]. This is the same as what you printed before the loop with:
std::cout << " [1] enter your first number: ";
so you see [1] twice.
To prevent this duplication, add 1 to the variable when displaying the prompt:
std::cout << " [" << (numbersEntered++) << "]" << " enter your next number or type '-1' to add them: ";
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
void Circle::getXYr() {
cout << "(x,y,R) = (" << x << "," << y << "," << r << ")" << endl;
}
double Circle::circArea() {
return (r * r * PI);
}
double Circle::circPeri() {
return (2 * r * PI);
}
void Circle::printCircle() {
cout << "Printing circle " << getXYr << endl;
cout << "Circle Area: " << circArea << endl;
cout << "Circle Perimeter: " << circPeri << endl;
}
I cant use the printCircle function: ERROR Error 1 error C3867: 'Circle::getXYr': function call missing argument list; use '&Circle::getXYr' to create a pointer to member
also the same for the next 2 functions.
It are functions, you have to call them as such. Note the brackets ()
cout << "Printing circle " << this->getXYr() << endl;
cout << "Circle Area: " << this->circArea() << endl;
cout << "Circle Perimeter: " << this->circPeri() << endl;
this-> is not explicitly needed.
Your second error is due this->getXYr() does not return a value. It returns void. There is no basic_ostream overload for it.
You either should put the logic of the getXYr() into the printCircle() function. Or call that function without calling cout on it:
cout << "Printing circle ";
this->getXYr();
cout << "Circle Area: " << this->circArea() << endl;
cout << "Circle Perimeter: " << this->circPeri() << endl;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi i have problem with c++
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
float a,b,wynik;
cout << "quotient" << endl
<< "..." << endl << endl
<< "quotient 2 numbers."
<< "\ndivisior does not equal 0"<< endl << endl;
cout << "a=";
cin >> a;
cout << "b=";
cin >> b;
if (b!=0)
cout << "\n" << a << " / " << b << " = " << a/b << "\n\n";
else
cout <<"\ndivisior does not equal 0!\n\n";
system("PAUSE");
return 0;
}
I must use for or while when someone try quotient number by 0.
If I understood correctly, you probably want to ask for input again if b is zero
You can have something like this :
do {
cout << "quotient" << endl
<< "..." << endl << endl
<< "quotient 2 numbers."
<< "\ndivisior does not equal 0"<< endl << endl;
cout << "a=";
cin >> a;
cout << "b=";
cin >> b;
if (b!=0)
cout << "\n" << a << " / " << b << " = " << a/b << "\n\n";
else
cout <<"\ndivisior does not equal 0!\n\n";
}while(b==0);
Your terms are wrong, see Wikipedia definition of "quotient".
#include <iostream>
int main(void)
{
char prompt[] =
"\n"
"Division, dividend / divisor = quotient:\n"
" Enter divisior: ";
int divisior;
int dividend;
while (true)
{
std::cout << prompt;
std::cin >> divisor;
if (divisor == 0)
{
std::cout << "\n* Divisor is zero, try again.\n";
continue;
}
std::cout << "\n Enter dividend: ";
std::cin >> dividend;
std::cout << "\nResult of "
<< dividend
<< " / "
<< divisor
<< " is, using integer division, "
<< dividend / divisor
<< "\n";
break;
}
return EXIT_SUCCESS;
}
I want to input data from txt file.
the file contains 2-d array [5][5]
how can i print out the any value i want?
i don't want to print out the whole 5*5 data
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
double distance[5][5] ;
string line;
ifstream ratefile;
ratefile.open("a.txt");
ofstream file;
if (ratefile.is_open())
{
while (! ratefile.eof() )
{
getline (ratefile,line);
ratefile.getline(distance, 25, '*');
cout << "\nDistance [0][0]" << ": " << distance[0][0];
cout << "\nDistance [0][1]" << ": " << distance[0][1];
cout << "\nDistance [0][2]" << ": " << distance[0][2];
cout << "\nDistance [0][3]" << ": " << distance[0][3];
cout << "\nDistance [1][0]" << ": " << distance[1][0];
cout << "\nDistance [1][1]" << ": " << distance[1][1];
cout << "\nDistance [1][2]" << ": " << distance[1][2];
cout << "\nDistance [1][3]" << ": " << distance[1][3];
cout << endl;
cin.get();
return 0;
}
If you only want to output one value and the user should be able to choose a value, you can do something like this:
int x, y;
cin >> x;
cin >> y;
cout << "\nDistance [" << x << "][" << y << "]" << ": " << distance[x][y];
But you should check if the user enter valid numbers (0 <= x < 4 and 0 <= y < 4)
There is part of the code missing, but you are printing values you want. Simply remove the lines you don't want to print.
Of course you can also use variables:
int x = 2,y = 2;
cout << endl << "Distance [" << x << "][" << y << "] : " << distance[x][y];