How to use an if statement in a string array? [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
The following code is supposed to randomly pick a string from the array and then say "Yay!" if Happy has been chosen.
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
int main()
{
srand(time(NULL));
string textArray[4] = { "Happy", "Sad", "Mad", "Overjoyed." };
int RandIndex = rand() % 4;
cout << textArray[RandIndex] << endl;
//if (textArray == "Happy") old
if (RandIndex == 0) //new
{
cout << "Yay!" << endl;
}
cin.get();
}
My problem is that the operand types are incompatible with strings and chars. What would be the best solution to this problem?
EDIT: All I needed to do was replace "if (textArray == "Happy")" with "if (RandIndex == 0)"

For example like
if ( textArray[RandIndex] == "Happy" )
{
cout << "Yay!" << endl;
}
or like
if ( RandIndex == 0 )
{
cout << "Yay!" << endl;
}
Also it would be better to write at least like
string textArray[] = { "Happy", "Sad", "Mad", "Overjoyed." };
const size_t N = sizeof( textArray ) / sizeof( *textArray );
size_t randIndex = rand() % N;

Related

Is a function terminates as soon as it returns a value, ignoring its next lines? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
What value the return_a_number function returns in following case:
#include <iostream>
#include <conio.h>
using namespace std;
int return_a_number(int n)
{
if(n < 0) return -1;
return 1;
}
int main()
{
cout << "Enter any number: ";
cin >> num;
cout << return_a_number(num);
getch();
return 0;
}
As return 1 is the last statement of function, Is it returns 1 everytime?
I'm new in c++. So I've many confusions.
If a function returns, it terminates there, following lines of code in that function will not be executed.
#include <iostream>
#include <string>
std::string foo(int x)
{
if(x < 0) return "If x < 0, this will be returned";
if(x == 0) return "If x == 0, this will be returned";
return "If x > 0, this will be returned";
}
int main() {
std::cout << foo(-1) << std::endl;
std::cout << foo(0) << std::endl;
std::cout << foo(1) << std::endl;
}
Output:
If x < 0, this will be returned
If x == 0, this will be returned
If x > 0, this will be returned

What exactly does this compiler error want me to perform? [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
Thank you in advance for reading ! So this is the code :
#include<iostream>
#include<vector>
std::vector<int> MonkeyCount(int n);
int main() {
MonkeyCount(4);
return 0;
}
std::vector<int> MonkeyCount(int n) {
std::vector<int> MonkeyCountV;
for (unsigned int i = 1; i <= n; i++) {
MonkeyCountV.push_back(i);
}
for (unsigned int i = 0; i <= MonkeyCountV.size(); i++) {
std::cout << MonkeyCount.at(i) << " ";
}
return MonkeyCountV;
}
and the error is on line 23 : error C2227: left of '->at' must point to class/struct/union/generic type
Now i red something about this, but i use this from an example i found on the internet on how to print a vector, and in that exaple, in works. The exaple is this :
#include <iostream>
#include <vector>
void print(std::vector<int> const& input);
int main()
{
std::vector<int> input = { 1, 2, 3, 4, 5 };
print(input);
return 0;
}
void print(std::vector<int> const& input)
{
for (unsigned int i = 0; i < input.size(); i++) {
std::cout << input.at(i) << ' ';
}
}
std::cout << MonkeyCount.at(i) << " ";
Should be:
std::cout << MonkeyCountV.at(i) << " ";
The way you have it is trying to do ".at(i)" on the function itself.
The name of your vector is MonkeyCountV but in the cout statement you are using MonkeyCount which is actually the name of your function. Make sure you have your variables name typed correctly.

illegal else with no matching if [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
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";
}

Run Time Check Failure #2 S 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 6 years ago.
Improve this question
I'm having trouble on a C++ program. It keeps giving me a Run Time Error # 2 - S. It builds fine though. It builds fine. Main is supposed to call two other functions, one which puts ints into an array, the other which reverses the numbers' positions on the arrays, essentially creating a 'reverse' array.
#include "stdafx.h"
#include <iostream>
#include <array>
using namespace std;
void storeArray(int[], int);
void flipArray(int[], int);
int main()
{
const int arraysize = 10;
int foo[arraysize]; // everyone seems to use foo so I thought I'd try it
storeArray(&foo[arraysize], arraysize);
cout << endl;
flipArray(&foo[arraysize], arraysize);
return 0;
}
void storeArray(int foo[], int arraysize)
{
int counter;
counter = 0;
while (counter < arraysize)
{
cout << "Enter number " << (counter+1) << " : ";
cin >> foo[counter]; counter++;
}
return;
}
void flipArray(int foo[], int arraysize)
{
int counter2;
int counter3;
counter2 = 0;
counter3 = 9;
for (counter2; counter2 <= 9; counter2++)
{
cout << "Value number " << (counter2 + 1) << " is " << foo[counter3] << endl;
counter3--;
}
cout << endl;
return;
}
Any help would be appreciated. Thanks!
storeArray(&foo[arraysize], arraysize);
This passes the address just past the end of foo
Instead use:
storeArray(foo, arraysize);
And:
void storeArray(int* foo, int arraysize)
Likewise for:
flipArray(&foo[arraysize], arraysize);
EDIT: just noticed you're #include <array> and using C-style arrays. Not the C++ STL arrays defined in #include <array>

trying to solve project euler number 14 [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 7 years ago.
Improve this question
I´m trying to solve project euler problem number 14 and i have the code almost ready, but it keeps me giving the wrong answer.. Why it doesn't count more steps?? Thanks, sorry for the lack of commentary..
#include <iostream>
int collatz_length(int number);
int main() {
using namespace std;
int size_sequence, max_sequence = 0, number_ = 1000000, num;
while (number_>1) {
size_sequence = collatz_length(number_);
if (size_sequence > max_sequence) {
max_sequence = size_sequence;
num = number_;
cout << "size " << size_sequence
<< " starting number " << num << endl;
}
number_--;
}
cout << "The longest sequence has "
<< max_sequence << " steps, starting from the number: " << num << endl;
cin.get();
return 0;
}
int collatz_length(int number) {
using namespace std;
int size_sequence = 0;
while (number > 1) {
if ((number % 2) == 0){
number /= 2;
}
else {
number = (3 * number + 1);
}
size_sequence++;
}
return size_sequence;
}
It is possible that 3*n+1 will overflow an int. Perhaps you should use a uint64_t?