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
This is the function I made:
void loading(bool wsound) {
if (wsound = true)
{
PlaySound(TEXT("sounds/intro.wav"), NULL, SND_ASYNC);
cout << "..........";
Sleep(1000);
cout << "..........";
Sleep(1000);
cout << "..........";
Sleep(1000);
cout << "..........";
Sleep(1000);
cout << "..........";
Sleep(1000);
cout << ".........." << endl;
Sleep(1000);
}
else if (wsound = false)
{
cout << "..........";
Sleep(1000);
cout << "..........";
Sleep(1000);
cout << ".........." << endl;
}
else {
cout << "An error occured" << endl;
cin.get();
system("exit");
}
}
So what this basically does is that it takes a bool if the value is true then it loads it with sounds if false then it loads it without sounds.
My problem is that in the main I placed a bool with the value of true then it worked though after calling it again with the value type of false it still loads it with sounds.
The code is like this:
//a bunch of code here
loading(true);
//a bunch of code here
loading(false);
//and more codes.........
This is not a check, this is an assignment: wsound = true
You should use something like wsound == true, however in an if statement it is enough to only use if (wsound), which is equal to if (wsound == true). For the false check you could use: if(!wsound).
EDIT: To understand what happens:
if(wsound = true) is a correct statement, because the assignment operator=(), used in this if statement, should in the most cases return a non-cost reference to the variable, which is already explained here.
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 4 years ago.
Improve this question
Debugger sais there is an incompatible iterator.
How i can solve this.
What can cause the problem.
Here is my main code
for (std::list<std::chrono::duration<double, std::milli>>
::iterator it = road.get_times().begin()
;it!= road.get_times().end();it++,z++) //error incompatible iterator
{
*road::file << "Samochod z drogi " << road.get_lane_number() << " pojechal ";
switch (road.get_direction())
{
case'S':
*road::file << "prosto" << std::endl;
break;
case'L':
*road::file << "w lewo" << std::endl;
break;
case'R':
*road::file << "w prawo" << std::endl;
break;
}
*road::file << "Jego czas stania w kolejce wyniosl ";
avarage_time += it->count() / 1000;
*road::file << round(it->count() / 1000) << std::endl;
}
function road.get_times ()
std::list< std::chrono::duration<double, std::milli>> get_times()
{
return times;
}
get_times returns by value, which means that every time you call it you get a new list object. This is what happens in your for loop. You call it 2 times and you get two objects. You can't compare iterators from two different objects. To fix this create one object by calling get_times just once:
auto times = road.get_times();
for (auto it = times.begin(); it != times.end(); ++it, z++)
//...
You also might want to pause and consider if returning by value is the right approach. I can't answer that for you since I don't know what times is and what is the structure of your program.
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 5 years ago.
Improve this question
It seems that my if statements are not working and I'm getting the "illegal else without matching if" error message. any help would be great, thanks.
#include "stdafx.h"
#include "windows.h"
#include "iostream"
using namespace std;
int igame = 0;
int main()
{
Sleep(1000);
cout << "welcome to the Wild Casino!";
Sleep(1000);
cout << "\nplease select a game to play. 1 for Slots, 2 for Roulette, and 3 for Blackjack: ";
cin >> igame;
if (igame == 1);
{
cout << "\nWelcome to Slots";
}
else if (igame == 2);
{
cout << "\nWelcome to Roulette";
}
else
{
cout << "\nWelcome to Blackjack";
}
Sleep(1000000);
return 0;
}
if (igame == 1);
You have an extra semicolon at the end - this is equivalent to
if (igame == 1) { }
Your code creates an ill-formed program:
if (igame == 1) { }
{ // block not attached to if
cout << "\nWelcome to Slots";
}
else if (igame == 2) { } // this else has no matching if
{
cout << "\nWelcome to Roulette";
}
else // this else has no matching if
{
cout << "\nWelcome to Blackjack";
}
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 5 years ago.
Improve this question
I've gone over many questions with the same or similar titles, I have changed the code in so many ways I can't even count.... I have an interesting problem.
I have a class for logging that is extremely simple and just writes stuff into a file. The exact same code works in the constructor, but will not work in the member function. I'm stripping out some irrelevant code, the rest is:
private:
std::string logfile_path_;
std::string program_name_;
std::string GetTimestamp() {
timeval tv;
gettimeofday(&tv, NULL);
char cTimestamp[24];
strftime(cTimestamp, sizeof(cTimestamp), "%F %T", std::localtime(&tv.tv_sec));
sprintf(&cTimestamp[19], ".%03d", (tv.tv_usec / 1000)); // write the miliseconds (microseconds/1000) into cTimestamp starting at the 20th character. %03d == pad with 0, for a minimum length of 3, an integer.
return cTimestamp; // function returns std::string so this will be implicitly cast into a string and returned.
}
public:
int log_level_;
SrxDsLog(std::string Logfile_path, std::string program_name, int log_level) {
log_level_ = log_level;
program_name_ = program_name;
logfile_path_ = Logfile_path;
std::ofstream logfile(logfile_path_.c_str(), std::ios::out | std::ios::app);
std::cout << "Logger started, Log file: " << logfile_path_ << std::endl;
logfile << "Logger started, Log file: " << logfile_path_ << std::endl;
return;
}
void WriteLog(std::string Log_message, int Severity = LOG_CRITICAL, std::string Function_name = "") {
if (Severity >= log_level_) {
std::cout << GetTimestamp() << "|" << program_name_ << "|" << Function_name << "|" << GetSeverity(Severity) << "|" << Log_message << std::endl;
std::ofstream logfile(logfile_path_.c_str(), std::ios::out | std::ios::app);
logfile << GetTimestamp() << "|" << program_name_ << "|" << Function_name << "|" << GetSeverity(Severity) << "|" << Log_message << std::endl;
}
}
The question is why is it working in the constructor, but the exact same code is not working in the member function. The std::cout is writing the exact same log message I want, but it's not appearing in the file. The file contains a single line every time the program is run.
In an amazingly unsatisfying turn of events I voted to close my question.
The problem was apparently caused by undefined behavior in unrelated code. And that because I did something that's defined in C++11 but is not in C++03. Apparently you can't call constructors from constructors in C++03....
Because of that, and because the question didn't include the code that was actually at fault, the question seems incredibly bad.
Please close.
int log_level_;
The constructor fails to initialize this class member.
Subsequently, the comparison with this class member results in undefined behavior.
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 7 years ago.
Improve this question
I was wondering why I get a compile error when I try to use std::cout in between, say, an if statement and else if statement.
For example:
if (condition)
{body}
std::cout << "hello world" << std::endl;
else if (condition)
{body}
Gives the error
error: 'else' without a previous 'if'
Let me get this right first:
You want to execute the cout statement in between the conditional no matter whether the condition was met or not, i.e. no matter whether the body of the if got executed or not.
As previous commenters noted, you cannot place something between the end of scope of the if-block and the else keyword.
What about approaching this by splitting up the if-else-if block into two separate if-blocks:
if (condition1) {
body1
}
cout << "hello world" << endl;
if (!condition1 && condition2) {
body2
}
That's why indentation is important
if (condition)
{ body
std::cout << "hello world" << std::endl;
}
else if (condition)
{
body
}
In your code, cout is outsite the if block, so no more else is expected.
You cannot add any executable code between if and else if other than the enclosed body of the if and else if loops.
if (firstCondition)
{
/*code for firstCondition*/
//code anything here
}
//not here #######
else if (secondCondition)
{
/*code for secondCondition*/
//code anything here
}
Correct is:
if (condition)
{
std::cout << "hello world" << std::endl;
}
else if (condition)
{body}
That would be an option for you.
if (fistCondition)
{
/*code for fistCondition*/
std::cout << "hello first" << std::endl;
}
else if (secondCondition)
{
/*code for secondCondition*/
std::cout << "hello second" << std::endl;
}
If you wanna call the cout in any case between the first and second condition, then avoid the else keyword and apply two if statements.
if (fistCondition)
{
/*code for fistCondition*/
std::cout << "hello first" << std::endl;
}
std::cout << "posterior to first and prior to second if statement" << std::endl;
if (secondCondition && !firstCondition)
{
/*code for secondCondition*/
std::cout << "hello second" << std::endl;
}
In this case, && !firstCondition emulates an else keyword for your purpose.
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
i have a easy struct
struct Test {
std::vector<int> values;
int value;
}
with overloaded << operator
inline std::ostream& operator<<(std::ostream& p, const Test& t)
{
p << "test: ";
for(size_t i = 0; i < t.values.size(); i++) {
std::cout << t.values[i] << " ";
}
p << " value: " << t.value << std::endl;
return p;
}
this works fine when i use the default output. But when i am using my boost logge, shown here Different boost log sinks for every class, it print the values inside my console and the rest inside my file. Anyone has an idea what happens there?
std::cout << t.values[i] << " ";
should be
p << t.values[i] << " ";