# Char Pointer Array cin Problem

• 11-03-2012
yurci
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; }```
• 11-03-2012
rags_to_riches
Code:

`char g[1];`
That makes no sense. Why not just

Code:

`char g;`
Have you worked through the tutorials on this site?
• 11-03-2012
yurci
Quote:

Originally Posted by rags_to_riches
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*
• 11-03-2012
whiteflags
Quote:

Originally Posted by yurci
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.
• 11-03-2012
yurci
Quote:

Originally Posted by whiteflags
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.
• 11-03-2012
whiteflags
Quote:

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.
• 11-03-2012
yurci
Quote:

Originally Posted by whiteflags
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.

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; }```
• 11-03-2012
whiteflags
Quote:

Originally Posted by yurci
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.
• 11-03-2012
yurci
Quote:

Originally Posted by whiteflags
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.