I have compilation errors to just simply output a cout message. Below is my code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
char letter = 'a';
short age = 10;
int cout = 575;
long numStars = 985632145;
float pi = 3.1;
double price = 89.65;
string season = "summer";
cout << "Letter: "<< letter << endl;
std::cout << "Age: " << age<< endl;
std::cout << "Cout: " << cout << endl;
std::cout << "Number Stars: " << numStars << endl;
std::cout << "Pi: " << pi << endl;
std::cout << "Price: " << price << endl;
std::cout << "Season: " << season;
system("pause");
return 0;
}
The errors I get are on the line:
cout << "Letter: "<< letter << endl;
I have tried reinstalling VS2015 but that didn't help.
You have a variable of type int called cout - this is not allowed given that you are using namespace std. Change this variable name to something else, and avoid the statement using namespace std.
std::cout is a "reserved type/keyword" so you cannot use it as a variable name.
Related
I'm making a test program for starting with C++ :)
It's showing wrong values after first print
This is the code (very simple)
#include "pch.h"
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
int varInt = 123456;
char varString[] = "DefaultString";
char arrChar[128] = "Long char array right there ->";
int * ptr2int;
ptr2int = &varInt;
int ** ptr2ptr;
ptr2ptr = &ptr2int;
int *** ptr2ptr2;
ptr2ptr2 = &ptr2ptr;
while(1){
cout << "Process ID: " << GetCurrentProcessId() << endl;
cout << "varInt (0x" << &varInt << ") = " << varInt << endl;
cout << "varString (0x" << &varString << ") = " << varString << endl;
cout << "varChar (0x" << &arrChar << ") = " << arrChar << endl;
cout << "ptr2int (0x" << hex << &ptr2int << ") = " << ptr2int << endl;
cout << "ptr2ptr (0x" << hex << &ptr2ptr << ") = " << ptr2ptr << endl;
cout << "ptr2ptr2 (0x" << hex << &ptr2ptr2 << ") = " << ptr2ptr2 << endl;
cout << "Press ENTER to print again." << endl;
getchar();
cout << "-----------------------------------" << endl;
}
return 0;
}
The expected results are obvious, as the code is published as is:
Process ID is integer so should return 12704 (or any int value) instead of 31a0
varInt it's also integer and should return 123456 instead of 1e240
1e240 is the same thing as 123456 in hex. The first iteration will print 123456 correctly but after you set the base flag of cout to hex mode, you need to set it back to dec to print 123456 again on the next loop.
cout << "varInt (0x" << &varInt << ") = " << dec << varInt << endl;
See here for documentation.
This is my .h, header file
#ifndef KINGDOM_H_
#define KINGDOM_H_
namespace westeros {
class Kingdom {
public:
char m_name[32];
int m_population;
};
void display(Kingdom pKingdom[], int kingdomElement, char nameOfKingdom);
}
#endif
This is my .cpp, source file
#include <iostream>
#include "kingdom.h"
using namespace std;
namespace westeros{
void display(Kingdom pKingdom[], int kingdomElement, char nameOfKingdom){
cout << "------------------------------" << endl;
for (int i = 0; i < kingdomElement; i++) {
**if(pKingdom[i].m_name == nameOfKingdom){** //it's giving me error right here, visual studio underlining red line below == sign saying operand types are incompatible
cout << "Searching for kingdom " << pKingdom[i].m_name << " in Westeros " << endl;
cout << "------------------------------" << endl;
cout << pKingdom[i].m_name << ", population " << pKingdom[i].m_population << endl;
}
else {
cout << "------------------------------" << endl;
cout << "Searching for kingdom " << nameOfKingdom << " in Westeros " << endl;
cout << "------------------------------" << endl;
cout << nameOfKingdom << " is not part of Westeros." << endl;
cout << "------------------------------" << endl;
}
}
}
}
and this is my main file trying to call it
#include <iostream>
#include "kingdom.h"
using namespace std;
using namespace westeros;
int main(void)
{
int count = 0; // the number of kingdoms in the array
Kingdom* pKingdoms = nullptr;
//allocating dynamic memory
pKingdoms = new Kingdom[count];
display(pKingdoms, count, "Mordor");
cout << endl;
display(pKingdoms, count, "The_Vale");
cout << endl;
delete[]pKingdoms;
pKingdoms = nullptr;
return 0;
}
Can anyone find what could be the problem?
Your problem is that pKingdom[i].m_name is a char[32], and the type of nameOfKingdom is char. You cannot compare a character array with a character.
which type would I use then?
std::string
My question is this, how do I save an int value in a while loop, my code is all about gambling, you start with 1,000 and you want to make the most amount of cash, but when I roll again my cash restores back to its original value that I set.
My code is this (Note I am new so do not laugh at how bad it is)
#include <cmath>
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
char again = 'Y';
int test;
int yes;
int CashW;
CashW = 1000;
int CashL;
CashL = 1000;
int yLose;
yLose = 500;
int xCash;
xCash = 1000;
int xRan;
srand(time(0));
xRan = rand() % 100 + 1;
cout << " Welcome to the Gambling Game!" << endl;
cout << " If the number is above 50 I win!" << endl;
cout << " If the number is below 50 you lose!" << endl;
while (again == 'y' || again == 'Y')
{
cout << " The Number I Choose Is: " << xRan << endl;
CashL = xCash - xCash - xCash;
CashW = xCash + xCash;
if (xRan < 50) {
cout << " You win, rats!" << endl;
cout << " The cash you started with was: " << xCash << endl;
cout << " The cash you have now is: " << CashW << endl;
cout << " Type 1 to play again, type 2 to close the game." << endl;
cin >> yes;
}
if (xRan > 50) {
cout << " I win, you lose!" << endl;
cout << " The cash you started with was: " << xCash << endl;
cout << " The cash you have now is: " << CashL << endl;
cout << " Type 1 to play again, type 2 to close the game." << endl;
cin >> yes;
}
if (yes == 1) {
cout << " Cool, a gambling man! Time to make some cash" << endl;
}
}
}
In your code you currently display either CashW or CashL depending on the gampbling result.
Unfortunately, you only print out the result and never store it into xCash. So at next iteration you start again with the same xCash value !
You can easily solve this by adding xCash = CashW; or xCash = CashL; just under the line in which you display the result.
You are never updating xCash with the amount of each win/loss. You are not generating a new random number on each loop iteration. And you are stuck in an endless loop because you never update the loop variable again.
Try something more like this instead:
#include <cmath>
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
const int CashW = 1000;
const int CashL = 1000;
int xCash = 1000;
int xRan;
char answer;
srand(time(0));
cout << " Welcome to the Gambling Game!" << endl;
cout << " If the number is above 50 I win!" << endl;
cout << " If the number is below 50 you win!" << endl;
do
{
xRan = rand() % 100 + 1;
cout << " The Number I Choose Is: " << xRan << endl;
if (xRan < 50) {
cout << " You win, rats!" << endl;
cout << " The cash you started with was: " << xCash << endl;
xCash += CashW;
cout << " The cash you have now is: " << xCash << endl;
}
else if (xRan > 50) {
cout << " I win, you lose!" << endl;
cout << " The cash you started with was: " << xCash << endl;
xCash -= CashL;
cout << " The cash you have now is: " << xCash << endl;
}
else {
cout << " dang, a draw!" << endl;
}
cout << " play again? " << endl;
cin >> answer;
if ((answer != 'y') && (answer != 'Y')) {
cout << " All done? Come back again another time!" << endl;
break;
}
cout << " Cool, a gambling man! Time to make some cash" << endl;
}
while (true);
return 0;
}
I have written this code from a book i am reading but my complier warns be that symbol cout and endl could not be resolved. Why is that.
#include <iostream>
#include <float.h>
int main()
{
cout << "float: " << endl
<< "stevilo decimalnih mest: " << FLT_DIG << endl
<< "natancnost stevila....: " << FLT_EPSILON << endl
<< "Najmanjse stevilo.....: " << FLT_MIN << endl
<< "Najvecje stevilo......: " << FLT_MAX << endl
<< "Bitov v mantisi.......: " << FLT_MANT_DIG << endl
<< "Najvecji eksponent....: " << FLT_MAX_10_EXP << endl
<< "Najmlajsi eksponent...: " << FLT_MIN_10_EXP << endl;
return 0;
}
You would have to use namespace:
#include <iostream>
#include <float.h>
using namespace std;
or:
std::cout
You can read more about namespaces in c++ here
cout and endl are scoped. So you have to inform in which namespace there are.
Use
std::cout
std::endl
or badly add just after include
using namespace std;
I've been trying a program from codeproject, about ptr_vector, and while compiling, the above error is shown.
Googling shows no hope to solve this problem. Could anyone here help out?
Here's the entire code (am compiling with gcc 4.2.2)
#include <iostream>
#include <string>
#include <boost/ptr_container/ptr_vector.hpp>
using namespace std; // for cout, endl, find, replace, ...
using namespace stdx; // for ptr_vector, ptr_vector_owner
using namespace boost;
int main()
{
cout << "---- ptr_vector demo ----" << endl;
ptr_vector<string> ptv;
ptr_vector_owner<string> owner (ptv); // scope-guard: owner of new-ed objects
ptv.push_back (new string ("Peter"));
ptv.push_back (new string ("Paul"));
ptv.insert (ptv.end(), new string ("Margaret"));
cout << " 1: " << ptv.front() << " " << ptv.back() << endl;
cout << " 2: " << ptv[1] << " " << ptv.at(2) << endl;
cout << " 3: " << *ptv.begin() << " " << *(ptv.begin() + 1) << endl;
cout << " 4:";
for (ptr_vector<string>::iterator it = ptv.begin(); it != ptv.end(); ++it)
cout << " " << *it;
cout << endl;
ptv.sort();
cout << " 5: " << ptv[0] << " " << ptv[1] << " " << ptv[2] << endl;
ptv.sort (greater<string>());
cout << " 6: " << ptv[0] << " " << ptv[1] << " " << ptv[2] << endl;
ptr_vector<string>::iterator iter;
iter = find (ptv.begin(), ptv.end(), "Paul");
if (iter != ptv.end())
cout << " 7: " << *iter << endl;
replace (ptv.begin(), ptv.end(), string ("Paul"), string ("Fred"));
cout << " 8: " << ptv.begin()[1] << endl;
string* str = ptv.pop_back();
cout << " 9: " << *str << " - size: " << ptv.size() << endl;
delete str;
delete ptv.detach (ptv.begin());
cout << "10: " << ptv[0] << " - size: " << ptv.size() << endl;
ptr_vector<string> ptvTwo;
ptr_vector_owner<string> ownerTwo (ptvTwo);
ptvTwo.push_back (new string ("Elisabeth"));
ptvTwo.push_back (new string ("Susan"));
ptv.swap(ptvTwo);
if (ptv < ptvTwo)
cout << "11: " << *ptv.begin() << " - size: " << ptv.size() << endl;
return 0;
}//main
stdx is not a standard namespace, it is defined by the particular implementation you are trying to use. You are not using the header file include #include "ptr_vector.h" inside which namespace stdx exists. Currently the ptr_vector you are using is being included from boost namespce. That begs the question, if you can use boost why do you want use stdx namespace solution.