Thread: Char Pointer Array cin Problem

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    Char Pointer Array cin Problem

    Hello Guys,

    I want to write a simple program about guitar scales!
    I want to store scales in program like: A B C D E F G as a CMajor and so on..

    And I'd like to input dynamic sized notes( like: C# D E or more ), and make program search for which scale could be belonging that notes. Waiting for one or more scales in return according my input size.

    So this is what i done so far, my problem is at inputing notes and assigning to the array. By the way i am not a newbie but a noob, Thanks for the anwers!

    Code:
    #include <iostream>
    #include <string>
    
    
    
    
    using namespace std;
    
    
    
    
    int main(int argc, const char * argv[])
    {
    
    
        int s=0;             
        char *gam[3][8];   // This is for storing SCALES, This size for now.
        char *sual[7];    // This is my input array for searching 
        char g[1];       // This cin my NOTE input and assigns it to sual[] array for further.  
    
    
    
    
        for(int i=0; i<3; i++)
            for(int j=0; j<8; j++)
                gam[i][j]="NULL";
    
    
        for(int i=0; i<7; i++)
            sual[i]="NULL";
    
    
    
    
        // HERE I WROTE THE SCALES THERE ARE 2 FOR NOW
    
    
        gam[1][0]="You're gam is C or Am";//the first slots for RETURN. To see which scale am i in.
        gam[1][1]="C";
        gam[1][2]="D";
        gam[1][3]="E";
        gam[1][4]="F";
        gam[1][5]="G";
        gam[1][6]="A";
        gam[1][7]="B";
    
    
        gam[2][0]="You're gam is D or Bm";
        gam[2][1]="D";
        gam[2][2]="E";
        gam[2][3]="F#";
        gam[2][4]="G";
        gam[2][5]="A";
        gam[2][6]="B";
        gam[2][7]="C#";
    
    
    
    
        cout << "Welcome SCALE FinDer!!..\n";
    
    
        cout << "Please Enter you're Scale you want to search..." << endl; // I input the notes one by one
    
    
        cin>>g;
        sual[s]=g; // here is my intial problem WHY i cannot assign cin directly to sual[s]. 
    
    
        while(s<6)// THIS is for maximum 7 notes are enough to determine.
        {
            s++;
            cout<< " Enter the other note..  IF YOU HAD ENTERED LAST NOTE ENTER X "<<endl;
            cin>>g;
            if(g=="X")// THIS IF IS NOT WORKING, ALWAYS DOES THE ELSE STATE
                break;
            else
                sual[s]=g;
    
    
    
    
        }
    
    
        //THS IS WHAT I DID SO FAR, AND IF I OUTPUT RESULTS.
    
    
        cout<<sual[0]<<sual[1]<<sual[2]<<sual[3]<<sual[4]<<sual[5]<<sual[6]<<endl;// THE ALL 7 WILL BE WHICH NOTE ENTERED LAST.
    
    
    
    
    
    
        return 1;
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    char g[1];
    That makes no sense. Why not just

    Code:
    char g;
    Have you worked through the tutorials on this site?

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Quote Originally Posted by rags_to_riches View Post
    Code:
    char g[1];
    That makes no sense. Why not just

    Code:
    char g;
    Have you worked through the tutorials on this site?

    If you do it so, it give a convertion error: char to char*

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by yurci View Post
    If you do it so, it give a convertion error: char to char*
    Symptoms of bigger data structure problems. An array of 1 really isn't an array at all.

    Code:
    char *gam[3][8];   // This is for storing SCALES, This size for now.
        char *sual[7];    // This is my input array for searching 
        char g[1];       // This cin my NOTE input and assigns it to sual[] array for further.
    My turn:
    Code:
    char *gam[3][8]; // declare gam as array, 3 of array, 8 of pointer to char
    char *sual[7]     // declare sual as array, 7 of pointer to char 
    char g[1]           // declare g as array, 1 of char
    All those pointers need to be pointing at useful addresses, and they make up the elements of a majority of your arrays. It is risky to store literal strings in such an obvious way because the minute you try to write to them, they invoke undefined behavior, because they (the literal strings, as in "G") are stored in read only memory.

    Since this is C++ you really ought to make your life easier:
    Code:
    #include <string>
    using std::string;
    
    string gam[3][8];
    string sual[7];
    string g;
    Of course, not that I'm suggesting you ignore C strings forever, lest you be fated to be satisfied with crappy algorithms because of a lack of foundational knowledge. When there are better things out there. In a way, string is C strings, contained. You should take advantage.
    Last edited by whiteflags; 11-03-2012 at 01:24 PM.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Quote Originally Posted by whiteflags View Post
    Symptoms of bigger data structure problems. An array of 1 really isn't an array at all.

    Since this is C++ you really ought to make your life easier:
    Code:
    #include <string>
    using std::string;
    
    string gam[3][8];
    string sual[7];
    string g;
    Of course, not that I'm suggesting you ignore C strings forever, lest you be fated to be satisfied with crappy algorithms because of a lack of foundational knowledge. When there are better things out there. In a way, string is C strings, contained. You should take advantage.
    Thank you, It solves my problem. And also it is same with char* gam[3][8] as well as string(I don't know why!) ;

    Code:
        char *gam[3][8];   // This is for storing SCALES, This size for now.
        string sual[7];    // This is my input array for searching 
        string g;       // This cin my NOTE input and assigns it to sual[] array for further.
    This handles my problem THANK YOU. And more for further knowledge recommendations as well.
    Last edited by yurci; 11-03-2012 at 01:50 PM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    And also it is same with char* gam[3][8] as well as string(I don't know why!) ;
    Well, like I said, string is C-string, contained. The goal of string was to replace C strings, but C++ as a language maintained C strings at the same time, so that it could compile C programs.

    But anyway, I already explained what gam is declared as. Since at some level you have pointers, and because arrays decay into pointers to the first element, you can happily do things like this:
    Code:
        gam[2][0]="You're gam is D or Bm";
        gam[2][1]="D";
        gam[2][2]="E";
        gam[2][3]="F#";
        gam[2][4]="G";
        gam[2][5]="A";
        gam[2][6]="B";
        gam[2][7]="C#";
    which is basically code for "My string starts at this address, and I want the address stored in x column of y row in the gam matrix." Not very useful since you still cannot edit the actual string without invoking undefined behavior; the same problem as before. If you don't intend to edit the strings, make the elements of your matrix const, and initialize your matrix the normal way instead.
    Last edited by whiteflags; 11-03-2012 at 02:35 PM.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Quote Originally Posted by whiteflags View Post
    which is basically code for "My string starts at this address, and I want the address stored in x column of y row in the gam matrix." Not very useful since you still cannot edit the actual string without invoking undefined behavior; the same problem as before. If you don't intend to edit the strings, make the elements of your matrix const, and initialize your matrix the normal way instead.

    THANKS FOR YOU'RE ANSWER.

    I make a step further and same problem continues like you said . I want to create an integer array and assign the number of same notes for each scale. And there is a run time error. I believe it is because of the thing you mentioned. How can i solve it for integer?

    Code:
    #include <iostream>
    #include <string>
     
     
     
    using std::string;
    using namespace std;
     
     
     
     
    int main(int argc, const char * argv[])
    {
     
        int gamsize=7;
        int s=0;             
        string gam[3][8];   // This is for storing SCALES, This size for now.
        string sual[7];    // This is my input array for searching 
        string g;       // This cin my NOTE input and assigns it to sual[] array for further.  
        int counter[2];// This will store number of same notes found for each scale. For now there are two
        int same=0;       // This is the number of same notes found
        
     
     
     
        for(int i=0; i<3; i++)
            for(int j=0; j<8; j++)
                gam[i][j]="NULL";
     
     
        for(int i=0; i<7; i++)
            sual[i]="NULL";
     
     
     
     
        // HERE I WROTE THE SCALES THERE ARE 2 FOR NOW
     
     
        gam[1][0]="You're gam is C or Am";//the first slots for RETURN. To see which scale am i in.
        gam[1][1]="C";
        gam[1][2]="D";
        gam[1][3]="E";
        gam[1][4]="F";
        gam[1][5]="G";
        gam[1][6]="A";
        gam[1][7]="B";
     
     
        gam[2][0]="You're gam is D or Bm";
        gam[2][1]="D";
        gam[2][2]="E";
        gam[2][3]="F#";
        gam[2][4]="G";
        gam[2][5]="A";
        gam[2][6]="B";
        gam[2][7]="C#";
     
     
     
     
        cout << "Welcome SCALE FinDer!!..\n";
     
     
        cout << "Please Enter you're Scale you want to search..." << endl; // I input the notes one by one
     
     
        cin>>g;
        sual[s]=g; // here is my intial problem WHY i cannot assign cin directly to sual[s]. 
     
     
        while(s<6)// THIS is for maximum 7 notes are enough to determine.
        {
            s++;
            cout<< " Enter the other note..  IF YOU HAD ENTERED LAST NOTE ENTER X "<<endl;
            cin>>g;
            if(g=="X")// THIS IF IS NOT WORKING, ALWAYS DOES THE ELSE STATE
                break;
            else
                sual[s]=g;
        }
    
    
    
    
    for(int i=1; i<3;i++)//for each gamut
    {
        for(int j=1; j<=7;j++)//for each note
        {
            for(int k=0; sual[k] !="NULL";k++)//for each input
            {
                if(gam[i][j]==sual[k]) // if find any note matches with a note in the scale, increase counter 
                    same++;
            }
        }
        //THE CODE WORKS TILL HERE
        counter[i]=same;// ASSIGN THE NUMBER OF MATCHES TO COUNTER
        same=0;// RESET THE COUNTER FOR THE LOOP for a new scale
    }
     
    
    
     
     
        return 1;
    }

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by yurci View Post
    I believe it is because of the thing you mentioned. How can i solve it for integer?
    Nope, now the cause is something totally different. Your loop condition is completely wrong: the valid range of indexes for any array is from 0 to size - 1 inclusive. So, your loops will have to work like that.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Quote Originally Posted by whiteflags View Post
    Nope, now the cause is something totally different. Your loop condition is completely wrong: the valid range of indexes for any array is from 0 to size - 1 inclusive. So, your loops will have to work like that.
    YES, sorry for my lack of attention.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read char by char into two-dimensional array pointer.
    By 'Serj Codito in forum C++ Programming
    Replies: 5
    Last Post: 09-08-2012, 09:47 AM
  2. About char pointer to char array
    By homoon in forum C Programming
    Replies: 3
    Last Post: 05-27-2012, 03:26 AM
  3. Copy char array to char pointer
    By Suseela in forum C Programming
    Replies: 9
    Last Post: 08-06-2009, 12:49 PM
  4. pointer to pointer that points to a char array
    By steve1_rm in forum C Programming
    Replies: 2
    Last Post: 01-14-2009, 12:03 AM
  5. Char-array vs Char-pointer
    By ripper079 in forum C++ Programming
    Replies: 12
    Last Post: 09-09-2002, 01:16 AM

Tags for this Thread