c++ - Indefinite size of an array -


this question has answer here:

i trying make simple program counts number of vowels , consonants user's name has. size of array dependent on how many letters user's name has. i'm using queue display letters. when ever run program, .exe file crashes. tried change array size number instead of variable , worked. problem in using variable size of array, think. there anyway fix it? can still use variable size of array. here code

#include <iostream> #include <queue>  using namespace std;  int main() {     int i;     char bin[i];     queue<char> name;     int v = 0, c = 0;      cout << "how many letters name have?:";     cin >> i;     cout << "enter letters of name 1 one:\n";      {         (int k = 0; k < i; k++) {             cout << " ";             cin >> bin[k];             name.push(bin[k]);              if (bin[k] == 'a' || bin[k] == 'a') {                 v++;             }             if (bin[k] == 'e' || bin[k] == 'e') {                 v++;             }             if (bin[k] == 'i' || bin[k] == 'i') {                 v++;             }             if (bin[k] == 'o' || bin[k] == 'o') {                 v++;             }             if (bin[k] == 'u' || bin[k] == 'u') {                 v++;             }             if (bin[k] == 'b' || bin[k] == 'b') {                 c++;             }             if (bin[k] == 'c' || bin[k] == 'c') {                 c++;             }             if (bin[k] == 'i' || bin[k] == 'd') {                 c++;             }             if (bin[k] == 'f' || bin[k] == 'f') {                 c++;             }             if (bin[k] == 'g' || bin[k] == 'g') {                 c++;             }             if (bin[k] == 'h' || bin[k] == 'h') {                 c++;             }             if (bin[k] == 'j' || bin[k] == 'j') {                 c++;             }             if (bin[k] == 'k' || bin[k] == 'k') {                 v++;             }             if (bin[k] == 'l' || bin[k] == 'l') {                 v++;             }             if (bin[k] == 'm' || bin[k] == 'm') {                 c++;             }             if (bin[k] == 'n' || bin[k] == 'n') {                 c++;             }             if (bin[k] == 'p' || bin[k] == 'p') {                 c++;             }             if (bin[k] == 'q' || bin[k] == 'q') {                 c++;             }             if (bin[k] == 'r' || bin[k] == 'r') {                 c++;             }             if (bin[k] == 's' || bin[k] == 's') {                 c++;             }             if (bin[k] == 't' || bin[k] == 't') {                 c++;             }             if (bin[k] == 'v' || bin[k] == 'v') {                 c++;             }             if (bin[k] == 'w' || bin[k] == 'w') {                 c++;             }             if (bin[k] == 'x' || bin[k] == 'x') {                 c++;             }             if (bin[k] == 'y' || bin[k] == 'y') {                 c++;             }             if (bin[k] == 'z' || bin[k] == 'z') {                 c++;             }         }     } while (bin[100] != '1');      cout << "\n\nyour name is:\n";     (queue<char> run = name; !run.empty(); run.pop()) {         cout << " " << run.front() << endl;     }     cout << "\nvowels= " << v;     cout << "\nconsonants= " << c << "\n";      cout << "\n\n\n\n";      system("pause");     return 0; } 

your code has undefined behavior. int i; creates uninitialized int named i. use uninitialized garbage variable in char bin[i];. have array undefined size.

at point can't go further. once undefined behavior invoked can no longer reason how code should work. if need array , not going know size until run time should use std::vector , push_back()


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -