2 Replies - 9377 Views - Last Post: 28 February 2012 - 09:56 PM Rate Topic: -----

#1 Akherousin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-December 11

"Expression result unused" error

Posted 28 February 2012 - 12:34 AM

Right, so I'm dealing with this very very tricky problem where I'm supposed to write a program that takes the beginning letter of a word and determines if it's a vowel, consonant, or a different character altogether and terminates input at a lone q.

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
int main ()
{
    cout << "Enter words (q to quit):\n";
    string str1;
    int vowel = 0, conso = 0, other = 0;
    getline (cin, str1);
    for (int i = 0; i < (str1.length() + 1); ++i)
    {
        if (str1[i] == 'a', str1[i] == 'e', str1[i] == 'i', str1[i] == 'o', str1[i] == 'u', str1[i] == 'A', str1[i] == 'E', str1[i] == 'I', str1[i] == 'O', str1[i] =='U')
        {
            ++vowel;
            while (str1[i] != ' ')
            {
                ++i;
                if (str1[i] != '\n')
                {
                    getline(cin, str1).get();
                    break;
                }
            }
        }
        else if(str1[i] == 'q')
        {
            if (str1[i + 1] == '\n')
                continue;
            else
            {
                ++conso;
                while (str1[i] != ' ')
                {    
                    ++i;
                    if (str1[i] != '\n')
                    {
                        getline(cin, str1).get();
                        break;
                    }
                }
            }
        }
        else if(isalpha(str1[i]))
        {
            ++conso;
            while (str1[i] != ' ')
            {
                ++i;
                if (str1[i] != '\n')
                {
                    getline(cin, str1).get();
                    break;
                }
            }
            
        }
        else
        {
            ++other;
            while (str1[i] != ' ')
            {
                ++i;
                if (str1[i] != '\n')
                {
                    getline(cin, str1).get();
                    break;
                }
            }
        }
    }
    cout << vowel << " words beginning with vowels\n";
    cout << conso << " words beginning with consonants\n";
    cout << other << " others\n";
    return 0;
}


That's what I've put up so far. I'm figuring part of the problem might be where it gives off an "expression result unused" error, which is at:

if (str1[i] == 'a', str1[i] == 'e', str1[i] == 'i', str1[i] == 'o', str1[i] == 'u', str1[i] == 'A', str1[i] == 'E', str1[i] == 'I', str1[i] == 'O', str1[i] =='U')


If that's not how you test for multiple characters in one statement, then how would you do it?

Is This A Good Question/Topic? 0
  • +

Replies To: "Expression result unused" error

#2 Hezekiah   User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: "Expression result unused" error

Posted 28 February 2012 - 01:25 AM

The commas don't do what you think. You should use ||s instead of the commas.

The or operator (||) returns true if one or both of its operands are true, or false otherwise:
(true || true) == true
(true || false) == true
(false || true) == true
(false || false) == false


The comma operator evaluates both its operands, but returns the right one's result:
int a = 0, b = 0;
cout << (a = 1, b = a + 1, a);
//will print 1; a is first set to 1, then b is set to a+1 and a is returned

This post has been edited by Hezekiah: 28 February 2012 - 01:27 AM

Was This Post Helpful? 1
  • +
  • -

#3 Akherousin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-December 11

Re: "Expression result unused" error

Posted 28 February 2012 - 09:56 PM

Thanks, I reworked the code a little, but I'm still coming up with a recurring problem. In order to terminate the program, I have to insert enter 4 times. This is the newly written code.

#include <iostream>
#include <cctype>
#include <cstring>
const int ArSize = 100;
using namespace std;
int main ()
{
    cout << "Enter words (q to quit):\n";
    char line[ArSize];
    char altline[ArSize];
    cin.getline(altline, ArSize);
    for (int z = 0; line [z] != ' ' && line [z + 1] != 'q'; ++z)
    {
        strcat(line, altline);
        cin.getline(altline, ArSize);
    }
    int vowel = 0, conso = 0, other = 0;
    for (int i = 0; line[i] != '\0'; ++i)
    {
        if (isalpha(line[i]))
        {
            if (line [i] == ' ' && line [i + 1] == 'q')
                break;
            if (line[i] == 'a' || line [i] == 'e' || line[i] == 'i' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u' || line[i] == 'A' || line[i] == 'E' || line[i] == 'I' || line[i] == 'O' || line[i] == 'U')
            {
                ++vowel;
                for (int j = 0; line[i] != ' '; ++j)
                    ++i;
            }
            else 
            {
                ++conso;
                for (int k = 0; line[i] != ' '; ++k)
                    ++i;
            }
        }
        else
        {
            ++other;
            for (int l = 0; line[i] != ' '; ++l)
                ++i;
        }
    }
    cout << vowel << " words beginning with vowels\n";
    cout << conso << " words beginning with consonants\n";
    cout << other << " others\n";
    return 0;
}


It determines whether the beginning character of a word has a vowel, consonant or a non-alphabetic character just fine, but I can't figure out why it would do this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1