Simple C++ error, "else without previous if" [closed] - c++

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'm just getting started with C++ and I've run into an error. It tells me that I have an "else" without a previous "If", but I do. I checked the case of the code and it looks basically just like the example from the website I'm learning from, it just has different sentences after all the couts, so I don't know what's wrong. Any help will be much appreciated.
int main()
{
int iOud;
cout<<"Type in your age: ";
cin >> iOud; //"a variable for the person's age
cin.ignore();
if (iOud < 20 );{
cout<< "A message\n";
}
else if (iOud > 40 ) {
cout << "A message\n";
}
else {
cout << "A message\n";
}
cin.get();
}

Drop the ; from if (iOud < 20 );{
if (iOud < 20 ); is a valid statement, so the compiler does not emit an error until it finds the invalid } else if. That's why the compiler error appears odd on first inspection.

The ; after if (iOud < 20) terminates the if statement.
Drop it, and you'd be fine

You ended the if(...) sentence with ; so it doesn't recognize it at all it, just sees the else if and drops the error. Drop the ; at if (iOud < 20 ); {.
And may I recommend using Switch/Case, if you are using multiple if sentences, but as Sean mentioned in the comment, this wouldn't apply for your example of comparing range of values.

Related

while exercise with return in c++ [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 3 years ago.
Improve this question
I've just started coding in c++ and now I have an exercise that I can't do because the code seems to not work.
I've to find the max and the min with a sequence of n numbers (in this case i already know that they are 4). I've to use while.
I've just started so I don't know how return properly works...
there aren't syntactical errors but when I run it ask me the number but then it says that the algorithm ends with 0 value.
Here's the code, if you can help me thank you!
#include <iostream>
using namespace std;
main ()
{ float mag,min,i,a;
mag=0;
min=0;
i=0;
while (1)
{
if (i<5)
{ cout<<"insert a number"<<endl;
cin>>a;
if (i = 0)
{ mag=a;
min=a;
}
else
{ if (a<min)
{ min=a;
}
else
{ if (a>mag)
{ mag=a;
}
}
}
i=i+1;
}
else
{ cout<<"maggiore= "<<mag<<endl<<"min= "<<min<<endl;
}
return 0;
}
system ("pause");
}
I see at minimum one problem:
if (i = 0)
This is assignment of i to 0 and compare the result of assignment, which is always false if you assign a 0.
I believe you want only compare and not assign, so you have to use:
if ( i == 0 )
The next problem is
return 0;
This will always end the current function, if the function is main(), it will terminate your program. In your case, you can simply remove the return statement, as in main it will return 0 by default if the function ends.
But if you use
while (1)
without any return, your program runs endless. I don't know what is the expected behavior.
Rest looks fine.
Hint: Your indentation is a bit special. :-)
1st it should be i==0 not i=0 in the if
2nd you should place that return 0 after that cout maggiore or it will close after the first loop
3rd you don't need that system pause there. It does literally nothing. You should either remove it or place it exactly before the return.

compiler issues with simple array program that wont build due to limitations in scope perhaps [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 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
this is my first question on stack overflow.. Im having compiler issues and wanted to figure out if anyone could help me find out why.
this is the unfortunate error message I recieve every time I attempt to compile.
//*************************************************************************
// This program uses array with a for loop control structure to prompt the user to enter their most convenient days of the week to work
#include <iostream>
using namespace std;
#include <array>
#include <cstddef>
int main() {
int whichDay = 0;
array< string, 7> daysOfweek =
{
"sunday",
"monday",
"tuesday",
"wednesday",
"thursday",
"Friday",
"saturday"
};
for (size_t x= 0; x< daysOfweek.size(); x++)
{
cout << daysOfWeek[x] << endl;
};
cout << "enter your work day: " ;
cin >> whichDay;
cout << daysOfWeek[whichDay] += 1
return 0;
}
This is your issue: cout << daysOfWeek[whichDay] += 1.
What would you expect the result of "monday" += 1 to be?
You probably just want to display the chosen day, so you just need:
cout << daysOfWeek[whichDay];
Well, you also may want to check that the input value is in the valid range :)

Why do I keep getting an "expected expression error" in my While Loop? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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
So I am trying to put a code together for my homework assignment and for some reason I keep getting an expected expression error in the condition of my while loop where I am putting the <= end_money part. The error shows up on the <= . This is the only place in my code where I am getting this error. If someone could pleaseeee help me, I would greatly appreciate it. I've been stuck for so long. Here is the snippet:
Edit: Also there is an ending brace for the while loop, i just forgot to paste it here.
int player_total = 0;
int dealer_total = 0;
int player_bet;
int card_value = 0;
const int end_money = 1000;
const int starting_money = 100;
string card;
string response;
while (player_bet >= 0 && <= end_money)
{
cout << "You have $100. Enter bet: ";
cin >> player_bet;
if (player_bet <= starting_money) {
return true;
}
else if (player_bet > starting_money) {
cout << "You only have $100 to bet. Enter bet: ";
}
Because that is not a valid expression.
Change this:
while (player_bet >= 0 && <= end_money)
To:
while (player_bet >= 0 && player_bet <= end_money)
Translation:
while player-bet is 0 or bigger, and also while player-bet is end-money or smaller.
Your original expression is roughly:
while player_bet is zero or bigger and also while (something unknown and not specified) is less than or equal to end-money.

Set is not removing the duplicates [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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 am having trouble putting a file of words into a set. I can read the file and the words go into the set but the set doesn't discard the repeated words. Here is the snippet of code that I believe is causing the problem.
using namespace std;
while(readText >> line){
set<string> wordSet;
wordSet.insert(line);
for (std::set<std::string>::iterator i = wordSet.begin(); i != wordSet.end(); i++)
{
cout << *i << " ";
}
}
the sample file is this
1
2
2
3
4
5
5
and the output is exactly the same
As stated in comments, you are not using the std::set correctly. You need to move it, and the for loop, outside of your while loop:
using namespace std;
set<string> wordSet;
while(readText >> line) {
wordSet.insert(line);
}
for (set<string>::iterator i = wordSet.begin(); i != wordSet.end(); i++) {
cout << *i << " ";
}

how to get correct answer merge 2 sorted arrays?! C++ [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 9 years ago.
Improve this question
i wrote a little algorithm for marge to sorted array. but i have problem with that.
#include <iostream>
using namespace std;
int main() {
// main function started form here:
int firstArray[10] = {1,3,5,7,9,11,13,15,17,19};
int secondtArray[10] = {2,4,6,8,10,12,14,16,18,20};
int mergedArray[20];
int firstCounter=0 , secondtCounter=0 , mergedCounter=0;
while(firstCounter < 10 && secondtCounter < 10){
if(firstArray[firstCounter] < secondtArray[secondtCounter]){
mergedArray[mergedCounter] = firstArray[firstCounter];
firstCounter++;
} else {
mergedArray[mergedCounter] = secondtArray[secondtCounter];
secondtCounter++;
}
mergedCounter++;
}
while(firstCounter < 10) {
mergedArray[mergedCounter] = firstArray[firstCounter];
firstCounter++;
mergedCounter++;
}
while(secondtCounter < 10) {
mergedArray[mergedCounter];
secondtCounter++;
mergedCounter++;
}
for(int j=0; j<20; j++){
//cout << mergedArray[j] << endl;
}
cout << mergedArray[19];
return 0;
}
in outpout for array mergedArray[19] i get something like this: 2686916!!!
i don't know why i get this value. how can i fix that. and why i get this value.
Typo in last while. You may increase your warning level to let your compiler show you your typo (warning: statement has no effect [-Wunused-value]).
while(secondtCounter < 10) {
mergedArray[mergedCounter];
secondtCounter++;
mergedCounter++;
}
should be
while(secondtCounter < 10) {
mergedArray[mergedCounter] = secondtArray[secondtCounter];
secondtCounter++;
mergedCounter++;
}
As pointed out by WhozCraig's comment, you're not assigning any value to mergedArray[19] because you left out the assignment part of the statement.
Since you haven't assigned a value, it's printing out whatever value happens to be at that memory address from previous usage. If you run your program (as it's currently written) several times, you'll see that the number there might change. Also, if you'd printed out the values in mergedArray before assigning anything, you'd see more such meaningless (to you in the current application) numbers.