while exercise with return in c++ [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 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.

Related

I have a problem with substr function(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 2 years ago.
Improve this question
Well, I am supposed to return the middle character of a word. If the word's length is odd, return the middle character. If the word's length is even, return the middle 2 characters.Like "test" => "es" or "testing" => "t".
And This is what I have wroten =>
#include <iostream>
std::string get_middle(std::string input)
{
if (input.length() % 2 == 0) {
return input.substr(1,2);
} else {
return input.substr(1,1);
}
};
int Main() {
get_middle("test");
get_middle("testing");
}
Look at your code, does it return the middle character (in either case)? No because you say input.substr(1,..) it returns the second character.
What you want is this for the odd case
return input.substr(input.length()/2, 1);
I'll leave you to work out the even case.
In fact if you are very clever about it you can have the same formula for both odd and even cases.
First, your main method is incorrect. Kindly change "Main" to "main" and return some integer value.
This is how you can achieve your task.
#include <iostream>
using namespace std;
std::string get_middle(std::string input)
{
if (input.length() % 2 == 0) {
return input.substr((input.length() / 2)-1, 2);
}
else {
return input.substr(input.length()/2, 1);
}
};
int main() {
//cout<<get_middle("test")<<endl;
cout<<get_middle("testing")<<endl;
cout << get_middle("test") << endl;
return 0;
}
Output:
t
es
Now first output is for odd string, and second is for even string.
I am guessing the problem is that you are not getting the middle characters (please state the problem next time). The first parameter should be the index of the first character you want in the substring, meaning input.length() / 2

unhandled exception, invalid parameter passed to a function that considers invalid paramaters fatal [closed]

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 5 years ago.
Improve this question
I'm using visual studio 2017, and coding under unreal engine coding standards, and its throwing an unhandled exception with invalid parameters passed to a function that considers them fatal. I can't figure it out, the VS2017 debugger is completely useless, and I'm pretty new to coding, can anyone throw me some suggestions? EDIT: The only thing that i can come close to finding is that it appears to be being cause by a string being out of range cause by an infinite loop somewhere in the function below.
FBullCowCount FBullCowGame::SubmitGuess(FText Guess)
{
// increment the turn number
MyCurrentTry++;
// setup a return variable
FBullCowCount BullCowCount;
// loop through all letters in the guess
int32 HiddenWordLength = MyHiddenWord.length();
for (int32 MHWChar = 0; MHWChar < HiddenWordLength; MHWChar++) {
// compare letters against the hidden word
for (int32 GChar = 0; GChar < HiddenWordLength; GChar++) {
//if they match then
if (Guess[MHWChar] == MyHiddenWord[MHWChar])
{
//increment bulls if they're in the same place
if (MHWChar == GChar) {
BullCowCount.Bulls++;
}
else {
BullCowCount.Cows++;
}
}
} //increment cows if not
}
return BullCowCount;
}
Your code comment says "loop through all letters in the guess", but your code loops through all letters of MyHiddenWord instead. That means that unless both Guess and MyHiddenWord have the exact same length, this:
if (Guess[MHWChar] == MyHiddenWord[MHWChar])
will at some point access an element of Guess that's out of range, and this is the likely cause of the exception, if FText happens to use a range-checked operator[].
What you probably want here is:
#include <algorithm>
// ...
auto HiddenWordLength = std::min(MyHiddenWord.length(), Guess.length());
It will limit the amount of letters to loop through to the shorter of the two strings.

Project Euler #5 - Why won't this while loop finish? [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 5 years ago.
Improve this question
If I change the code line "num++" to "num+=2520", the code runs fine and returns the correct answer, but I'd like to know why it doesn't run as is, primarily because I didn't think of the fact that the number must be a multiple at 2520 before looking the answer up, and I don't see why my own code isn't correctly giving the answer without that change. To me, it seems correct. Unfortunately, the while loop never ends.
My guess is it has something to do with how long the correct number is (232792560), because if I lower the requirements even a little bit (from 9 to 8, per se), the while loop manages to finish.
long long int num = 1;
int div_counter = 1;
bool check = false;
while(!check)
{
for(int i = 2; i < 21; i++)
{
if(num % i == 0)
{
div_counter++;
}
}
if(div_counter == 20)
{
check = true;
}
else
{
num++;
div_counter = 0;
}
}
return num;
You have to reset div_counter to 1 instead of 0.
Your for loop only runs from 2 to 20 inclusive, so if div_counter starts at 0 the max value it can reach is 19.

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.

continue statement inside for loop and if condition [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have following code snippet and the output i am getting is 4. Please explain me if it takes i=2 or 0. I am confused. And How output was 4?
int main() {
int i=2;
for(i=0;i<2;i++) {
i=i%3;
if(i==2) {
i++;
continue; }
else
++i;
}
printf("%d",i);
}
The loop starts with i = 0. Both the if and the else to exactly the same thing. Increment i and continue.
If you use a bit of logic, the whole block can be reduced to i++ (i = i % 3 has no effect since i < 2).
It's not possible to get 4 with the code you posted.
The output cannot be 4 for the program you posted, because by the time the loop breaks, the value of i would be 2, not 4 and the loop will run exactly once.
Also, your code never enters the if block, because the condition is i==2 which can never be true inside the for loop, as by that time the loop would be exited.
So your code is equivalent to this:
int main() {
int i=2;
for(i=0;i<2;i++) {
i++;
}
printf("%d",i);
}