6 Replies - 348 Views - Last Post: 12 September 2011 - 04:24 AM Rate Topic: -----

#1 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Class Function Always Defaults back to "else" statment

Posted 11 September 2011 - 03:08 PM

I have here a class i wrote to get a given music scale. It assigns the Key just fine (i know because it does not return "error") but it ALWAYS returns "error" on mode. what have i done wrong?(The Place where i am having problems is between the "set mode" remarks)
#ifndef _MUSICKEY_H
#define _MUSICKEY_H
#include<string>
using namespace std;
class musickey
{
    public:
        musickey();
        musickey(const musickey &);
        string GetScale(const string &,const string &);
    private:
        string chromatic[12];
        string formula;
        string result;
        string mode;
        string scale;
        int last, next;
};

musickey::musickey()
{
}

musickey::musickey(const musickey &Object)
{
}
string musickey::GetScale(const string &inckey,const string &incmode)
{
    //initialize everything
    const string chromatic[12]=
    {
        "A","A#","B","C","C#","D","D#","E","F","F#","G","G#"
    };
    //initialize everything

    //SetKey
    int i;
    for(i = 0; (chromatic[i] != inckey) && (i < 12) ; i++)
    {
        last = i+1;
    }
    if(i == 12)
    {
        return "\nError\nInvalid Key";
    }
    //SetKey

    //SetMode
    if(incmode == "Major")
    {
        formula = "wwhwwwh";
    }

    if(incmode == "Minor")
    {
        formula = "whhhwhh";
    }

    if(incmode == "Dorian")
    {
        formula = "whwwwhw";
    }

    if(incmode == "Phrygian")
    {
        formula = "hwwwhww";
    }
    else
    {
        return "\nError\nInvalid mode.";
    }
    //SetMode

    //GetScale
    scale = "";
    for(unsigned int i = 0; i < formula.length(); i++)
    {
        scale.append(chromatic[last]);
        scale.append(" ");
        if(formula[i] == ('W' || 'w'))
        {
            next = 2;
        }
        if(formula[i] == ('H' || 'h'))
        {
            next = 1;
        }
        next += last;
        if(next > 11)
        {
            next -= 12;
        }
        last = next;
    }
    //GetScale
return scale;
}
#endif


This post has been edited by Ty Meador: 11 September 2011 - 03:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Class Function Always Defaults back to "else" statment

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1574
  • View blog
  • Posts: 2,996
  • Joined: 30-May 10

Re: Class Function Always Defaults back to "else" statment

Posted 11 September 2011 - 03:20 PM

> if(formula[i] == ('W' || 'w'))
You need to say
if( (formula[i] == 'W') || (formula[i] == 'w') )
Was This Post Helpful? 2
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Class Function Always Defaults back to "else" statment

Posted 11 September 2011 - 03:25 PM

Please show the value for incmode if it is not: "Phrygian" then you will report an error. Look closely at your if statements.

Jim
Was This Post Helpful? 1
  • +
  • -

#4 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Re: Class Function Always Defaults back to "else" statment

Posted 11 September 2011 - 03:31 PM

View PostSalem_c, on 11 September 2011 - 03:20 PM, said:

> if(formula[i] == ('W' || 'w'))
You need to say
if( (formula[i] == 'W') || (formula[i] == 'w') )

This isn't the issue im having.
and our code both achieve the same purpose, mine is legal. i use it all the time.

View Postjimblumberg, on 11 September 2011 - 03:25 PM, said:

Please show the value for incmode if it is not: "Phrygian" then you will report an error. Look closely at your if statements.

Jim

Thank you very much. in my Highschool C++ class we have very out dated books and IDE's. im using code::blocks, and was unaware that "else if" was now legal in C++, it wasnt in Borland Turbo C++ (DOS)
Was This Post Helpful? -5
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Class Function Always Defaults back to "else" statment

Posted 11 September 2011 - 03:33 PM

Quote

This isn't the issue im having.
and our code both achieve the same purpose, mine is legal. i use it all the time.

While it is "Legal" (it will compile) it will not produce the correct results.

Jim

This post has been edited by jimblumberg: 11 September 2011 - 03:35 PM

Was This Post Helpful? 4
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1574
  • View blog
  • Posts: 2,996
  • Joined: 30-May 10

Re: Class Function Always Defaults back to "else" statment

Posted 11 September 2011 - 11:24 PM

> This isn't the issue im having.
> and our code both achieve the same purpose, mine is legal. i use it all the time.
Then you're just bone-headed stupid.
Sure it compiles, but you can be damn sure it doesn't do what you want it to do.

If you're just going to -ve everybody who points out problems which don't seem to be the one in your imagination, then you're going to be on your own.

As for the rest of the mistakes - figure it out yourself.
Was This Post Helpful? 3
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Class Function Always Defaults back to "else" statment

Posted 12 September 2011 - 04:24 AM

View PostTy Meador, on 11 September 2011 - 06:31 PM, said:

This isn't the issue im having.
and our code both achieve the same purpose, mine is legal. i use it all the time.


Seriously? WTF? Do you even understand what it's doing? You clearly aren't listening to any advice you're getting, so why do you ask?

Did you ever test your theory?
#include <iostream>

using namespace std;

int main() {
	char ch = 'w';
	cout << boolalpha 
		<< "ch = " << ch << endl
		<< "('W' || 'w') = " << ('W' || 'w') << endl
		<< "(ch == ('W' || 'w')) = " << (ch == ('W' || 'w')) << endl
		<< "(ch == 'W') = " << (ch == 'W') << endl
		<< "(ch == 'w') = " << (ch == 'w') << endl
		<< "((ch == 'W')||(ch == 'w')) = " << ((ch == 'W')||(ch == 'w')) << endl
		;
}



Results:
ch = w
('W' || 'w') = true
(ch == ('W' || 'w')) = false
(ch == 'W') = false
(ch == 'w') = true
((ch == 'W')||(ch == 'w')) = true



If you choose to ignore the guidance offered to you, then you will never venture from the path you've already chosen. Good luck with that.
Was This Post Helpful? 4
  • +
  • -

Page 1 of 1