Fix program thats supposed to find two middle characters of string.

Sep 27, 2012 at 3:25am
Basically I feel like I've almost got it figured out. But for some reason I always get "expected primary-expression before "else"" and "expected ';' before "else"" I really don't understand why it doesn't work and I feel like my logic works. Maybe it doesn't? Could really use an explanation here! **Please note orginally I had the program set up to tell me whether the string had an odd or even number of characters so in the main there are some things that shouldn't be there but now I'm mostly concerned with the function.

#include <iostream>
#include <string>
using namespace std;

string even(string str)
{
       string o;
       string e;
       int d;
       int i = str.length(); //# of digits per string.
       if ((i % 2) == 0)
          {
              int d = i/2;
              e = str.substr(d,(d + 1));
          }
       return e; //This is even.
       else
          { 
              int d = i/2;
              o = str.substr (d);
          }
       return o; // This is odd.        
}

int main()
{
string input;
string x;
string y;
cout << "Please enter a number to see if its even or odd: ";
cin >> input;
cout << even(input) << endl;
system ("pause");
return 0;
}
Sep 27, 2012 at 3:36am
your return statements in the even function need to be inside the if/else statements
Sep 27, 2012 at 4:23am
hey thanks that worked! it compiled but when I enter in a word it doesn't output like I want it too. for example i enter in haggle and it outputs "gle" or dog and i get "og" what am I doing wrong? ideally the output should be "gg" for haggle and "o" for dog
Sep 27, 2012 at 2:08pm
So, you want a function named 'even' to return a substring that consists of the center-most character(s) of the supplied string after asking the user to "enter a number to see if it's even or odd?"

Am I seeing things?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
using namespace std;

std::string get_center(const std::string & s )
{
    if ( s.length() == 0 )
        return std::string() ;

    const bool length_is_even = s.length()%2 == 0 ;

    std::size_t substr_length ;
    std::size_t start_pos ;

    if ( length_is_even )
    {
        substr_length = 2 ;
        start_pos = (s.length() / 2) - 1 ;
    }
    else
    {
        substr_length = 1 ;
        start_pos = s.length() / 2 ;
    }

    return s.substr(start_pos, substr_length) ;
}

int main()
{
    string input;
    cout << "Enter a string: ";
    cin >> input;
    cout << get_center(input) << endl;
    system ("pause");
    return 0;
}


Note that string::substr takes two arguments. The second one is the length of the string to extract. If you don't supply the second argument, it is assumed that you want the substring from the position indicated to the end of the string.
Sep 27, 2012 at 5:07pm
yeah works perfectly! just one question

std::string get_center(const std::string & s )
{
    if ( s.length() == 0 )
        return std::string() ;

    const bool length_is_even = s.length()%2 == 0 ;


why do these bolded have to constants?
Sep 27, 2012 at 5:26pm
Topic archived. No new replies allowed.