error: expected unqualified-id before if [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
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.
Improve this question
I have looked through previous answers, but none of them have explained why I am getting this error.
Here is my code with the error. It occurs on "if(pathID==2...)" and every if statement after that.
void add_path(int a,int b, int current_step,int pathID){
if(pathID == 0){
path[current_step] = new step(a,b,"Filled A",path[current_step]);
}
if(pathID == 1)
path[current_step] = new step(a,b,"Filled B",path[current_step]);
}
if(pathID == 2){
path[current_step] = new step(a,b,"Empty A",path[current_step]);
}
if(pathID == 3){
path[current_step] = new step(a,b,"Empty B",path[current_step]);
}
if(pathID == 4){
path[current_step] = new step(a,b,"Pour B to A",path[current_step]);
}
if(pathID == 5){
path[current_step] = new step(a,b,"Pour A to B",path[current_step]);
}
}
All this code is meant to do is add to the linked list at a given position in an array. The pathID is passed in and tells it what action was performed, so we know we know what to add to the linked list.
Later in the program I use that linked list to determine what actions were taken. I still need to make it a doubly linked list so it does not print in reverse, but that's another problem.

You forgot a curly brace after
if(pathID == 1)
Add it and it'll work fine.

Related

For loop only executing once? [closed]

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 1 year ago.
Improve this question
I don't know if I'm just being stupid, but Visual Studio says that my for loop is only executing once, and it does seem to be the case. However I can't figure out why. I'd really appreciate it if anyone could tell me what I'm doing wrong.
for (int i = (nbToVerify - 1); i == 1; i--)
{
if (nbToVerify % i == 0)
{
nbIsPrime = false;
break;
}
else
{
nbIsPrime = true;
}
}
Thanks!
i == 1 should be i != 1, that's it.

How to fix input and output problem with files in C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
I can't find a problem with my code. It's loading in DEV C++ but after that the window with "Program has stopped working" pop up.
fstream file;
file.open("dane1.txt");
string linia;
string tab[5];
int i = 0;
do
{
getline(file,linia);
cout<<linia<<endl;
tab[i]=linia;
i++;
}
while(!file.eof());
file.close();
ofstream file2("wynik.txt");
if (file2)
{
for(int i=5;i>0;i--)
{
file2<< tab[i];
file2<< endl;
}
}
else
{
cout<<"You have problem with file!"<<endl;
}
pliki.close();
I want to get lines from 1st file (dane1.txt) and then put it in diffrent order in fail "wyniki.txt"
string tab[5];
// ...
for(int i=5;i>0;i--)
{
file2<< tab[i];
file2<< endl;
}
The first iteration of this for loop attempts to access tab[5] which, of course, doesn't exist since the five-element tab array contains only tab[0] through tab[4]. Undefined behavior, and the near-certain reason for your program crashing.

C++ comparing string elements to an int [closed]

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 have number lying in a string variable.
I wanna check if every one of its elements is equal to some value, so I use the for loop to loop over every element and use if:
int zera = 0, jedynki = 0;
for (int i = 0; i < liczba.length(); i++) {
if (liczba[i] == 0) zera ++;
else if (liczba[i] == 1) jedynki ++;
}
liczba is a string.
I know now that I can't do that. I tried to convert this int into char but still, nothing happened.
What's wrong here? What should I do?
you're comparing int with char
should be:
if (liczba[i] == '0') {}
else if (liczba[i] == '1') {}
should be used:
if (liczba[i] == '0')
or using atoi:
if (atoi(liczba[i]) == 0)

C++ code not doing what I want [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
When I call this function it doesn't display any *. But when I change == to < it works. Why?
void starBox(int size){
for(int i = 0; i == size; i++){
for(int j = 0; j == size; j++){
cout << '*';
}
cout << endl;
}
}
I think your problem is due to misunderstanding of what the condition in the middle of for loop means. This is a loop continuation condition, meaning that the loop needs it to be true to proceed. Loop's post-condition is the inverse of its continuation condition.
When I call this function it doesn't display any *.
This is because the loop continuation condition is false unless size is zero, so loop body is skipped.
But when I change == to < it works. Why?
Because the loop continuation condition becomes true, and stays true until the loop is over. That is why < works; != would work as well.

Linked List delete function exception [closed]

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 some study to do for an upcoming C++ assessment and I have an issue with my deleteNode function. I know the problem lies somewhere within the function as I copied a previous deleteNode function and it worked fine.All of my other pushback/pushfront functions work fine, I'd rather know the problem than just ignore it but I can't spot it for the life of me.
So the linked list is storing Employees, with a name (string) and a salary (double). My deleteNode matches the name of the employee and the string parameter passed in. If anyone can spot my mistake it'll be a great help to my learning!
bool EmployeeList::deleteNode(std::string n){
EmployeeNode *leadptr = head, *trailptr = nullptr;
if (head != nullptr){
if (head->emp.name == n){
head = head->next;
delete leadptr;
return true;
}
else{
while (leadptr != nullptr && leadptr->emp.name != n){
trailptr = leadptr;
leadptr = leadptr->next;
}
if (leadptr = nullptr){
return false;
}
else{
trailptr->next = leadptr->next; //access violation here, leadptr may be null
delete leadptr;
return true;
}
}
}
}
This is an assignment
if (leadptr = nullptr){
Correction would be
if (leadptr == nullptr){