Determining Size of Array

Hello Everyone,
char A[]={};
this is the array in c++ of unknown size, now I want to enter some alphabets via loop and want to be the no. of elements i entered the size of the array. . . ! is it possible??? If yes then how??
This will work if you use an STL vector, rather than a C-style array. As you add elements to the vector (using push_back()), the vector increases its storage automatically to hold them.
will you please write an example code for the above vector as I'm beginner i don't have a clear idea about vectors in c++.
Last edited on
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <conio.h>
using namespace std;
char y;
int main(){
    int l=0,b=0;
    char A[]={}, r='y';
    int choice; 
    cout<<" \t   *Champ's Coding LAB*"<<endl<<endl;
    cout<<" \t  S.SaRa Tirmizi Presents"<<endl<<endl; 
    cout<<"\t   Code Maker & Breaker ~ (CMB v2.0)"<<endl<<endl;
    while (r=='y') {
    cout<<"Enter 1 for Convert Your Word into Code"<<endl; 
    cout<<"Enter 2 for convert code into meaningful word"<<endl;
    cout<<"Select Your Choice: ";
    cin>>choice;
    switch(choice){
                   case 1:   
                        cout<<"Enter length of the word: "; 
                        cin>>l;
                        cout<<"Enter the word to be converted in Code: ";
                        for (int i=0; i<b; i++){
                        
                        cin>>A[i];
                        }
                        for (int j=0; j<b; j++){
                            for (int i=0; i<=j; i++){
                                if (A[i]=='a'){                  A[i]='Z';
    }
    if (A[i]=='e'){
              A[i]='X';
    }
      if (A[i]=='i'){
              A[i]='C';
    }
      if (A[i]=='o'){
              A[i]='V';
    }
      if (A[i]=='u'){
              A[i]='B';
    }
    else{
              A[i]=A[i];
 }
       }
      }
      break;
      case 2:
           cout<<"Enter length of the word: ";
                        cin>>l;
                        cout<<"Enter the code word (Case Sensitive) to be converted: ";
                        for (int i=0; i<l; i++){
                        cin>>A[i];
                        }
                        for (int j=0; j<l; j++){
                            for (int i=0; i<=j; i++){
    if (A[i]=='Z'){    
              A[i]='a';
    }
    if (A[i]=='X'){
              A[i]='e';
    }
      if (A[i]=='C'){
              A[i]='i';
    }
      if (A[i]=='V'){
              A[i]='o';
    }
      if (A[i]=='B'){
              A[i]='u';
    }
    else{
              A[i]=A[i];
 }
 }
      }
      }
      cout<<"Coded Word is: ";
      for (int i=0; i<l; i++){
          cout<<A[i];
          }                  
 cout<<endl<<"Enter Y to Continue or E for Exit"<<endl;
     cin>>r;
     }
 getch();
 return 0;
}
// this is my code here instead of length i want to use syntax that will automatically count the length of characters i enter 
Last edited on
Edit your post to use [code] tags, please. No one wants to read that the way it is.
oh so sorry, now done with code tags
Must you use arrays?
If you use std::string a lot of your problems are solved for you.

Arrays must have a length

7
8
    char A[]={}
    ...

This does not declare an array of unknown size -- not the way you are thinking it does. You must give your arrays dimension.

char A[1000] = {'\0'};

That is an array of 1000 elements, all initialized to zero.


The user will always press ENTER at the end of every input.

Never forget it. So after you get things like choice, make sure to read the ENTER key (newline):

 
#include <limits> 
1
2
cin >> choice;
cin.ignore( numeric_limits<streamsize>::max(), '\n' );

Sorry it isn't prettier, but this is how you read the character and the newline.

Do the same thing when you read r:

1
2
cin >> r;
cin.ignore( numeric_limits<streamsize>::max(), '\n' );



Read a c-string

To read a string from the user, you can simply say:

cin.getline( A, 1000 );

(This is similar to how you would do it with a std::string, except that this input is limited to 999 characters, and you don't know if there is more...)


Get the length of A with the C library strlen() function:

 
#include <cstring> 
 
l = strlen( A );



Loops

I'm not sure why you have a nested loop going to iterate over a single string. Just use one loop.

1
2
3
4
5
6
			for (int i=0; i<l; i++){
			    if (A[i]=='a'){
				A[i]='Z';
			    }
			   ...etc
			}


if...else if...else

Use a switch.

There is no need to say A[i] = A[i];.


Printing a string

All you need to print is cout << A << "\n";


Variable names

You are not being very careful how you name your variables. For this reason you are being confused.

  l is a particularly bad name for a variable. Is it a 1 or an l? What does 'l' mean?

    Choose a more descriptive name, like "length".

  b In your code, you have a variable named "b", which does nothing. Don't you mean "length"?

  y does nothing. Get rid of it. (It shouldn't be global either.)

  r You probably named this "r" because you couldn't name it "y", right? Why not "repeat" or something like that?

  choice Good name. User's choice from the menu options.


Indentation

Watch your indentation too.


Hope this helps.
thanks a lot for the suggestions, strings solved my problem and also reduced the lines of the code. (Y)
Topic archived. No new replies allowed.