Deque problem.

Hello. I have a code in which I am trying to store the position in a sentence in a deque.

This is what i've figured so far.

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

int main()
{

    char iword[200];
    string iword2, astring;
    int ilength=0, acount=0, ecount=0, icount=0, jcount=0, dcount=0;

    iword2=iword;
    ilength=iword2.length();
    cin.get(iword, sizeof(iword)); cin.ignore(1000, '\n');


deque<int>a_deque;

    for(int i=0; i<ilength; i++){
            if(iword[i]=='H'){
    acount++;



    a_deque.push_front(i);

}
}
int lol=a_deque.max_size();
cout<<lol;
for(int i=0; i<a_deque.max_size(); i++){
        try{cout<<"\n"<<a_deque.at(i);}
        catch(exception& e){
                         cout<<"Element "<<i<<" exceeds deque dimensions "<<endl;
                        
                        }
        
        
        }
cout<<a_deque.at(1);





    return 0;
}


This is just the relevant part though. When I am trying to actually run this I get no errors until the deque starts to run. It never stops as it should do. When I checked what type of deque.max_size() was it returned -1 when one H was typed in. What am I missing?
max_size() doesn't return the vector's size (the name kinda implies that), but the largest theoretically possible size. The correct function is size().
Last edited on
heh, okey. However now the size() is stuck at 0, even though I input a H. :/
Should work fine if you're entering H.

H
1
terminate called after throwing an instance of 'std::out_of_range'
  what():  deque::_M_range_check
0Aborted


Edit: ne555's remark is spot on. It's pure chance that it gave the expected output for me.
Last edited on
The code is executed sequentially from top to bottom.
Read your code.

Side note: Learn to indent code. And with that I meant 'get an IDE and let it to indent the code'
Yeah I know that it reads from top to bottom. But I dont understand what you mean with indent code.

Sorry if I am slow.
This is how your code looks properly indented by Code::Blocks's code formatting:
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
int main()
{
  char iword[200];
  string iword2, astring;
  int ilength=0, acount=0, ecount=0, icount=0, jcount=0, dcount=0;

  iword2=iword;
  ilength=iword2.length();
  cin.get(iword, sizeof(iword));
  cin.ignore(1000, '\n');

  deque<int> a_deque;

  for(int i=0; i<ilength; i++)
  {
    if(iword[i]=='H')
    {
      acount++;
      a_deque.push_front(i);
    }
  }

  int lol=a_deque.size();
  cout << lol;
  for(int i=0; i<a_deque.size(); i++)
  {
    try
    {
      cout << "\n" << a_deque.at(i);
    }
    catch(exception& e)
    {
      cout << "Element " << i << " exceeds deque dimensions " << endl;
    }
  }
  cout << a_deque.at(1);
}


http://en.wikipedia.org/wiki/Indent_style
http://en.wikipedia.org/wiki/Indent_style
Look at your loop at line 16. It is not clear that those statements are inside the conditional.

About your error
1
2
3
4
5
6
7
8
9
10
11
12
13
    char iword[200]; //uninitialized (contains garbage)
    string iword2, astring;
    int ilength=0, acount=0, ecount=0, icount=0, jcount=0, dcount=0;

    iword2=iword; //using an uninitialized variable, undefined behaviour
    ilength=iword2.length();

    cin.get(iword, sizeof(iword)) cin.ignore(1000, '\n'); //now you are reading the input

    deque<int>a_deque;
//HERE ¿what is the value of 'ilength'? ¿how is it related to 'iword' that you just read?
    for(int i=0; i<ilength; i++){
       if(iword[i]=='H'){
As stated before you cant see the entire code as it was irrelevant, at least I though so
.

It is here:

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include "string.h"
#include <fstream>
#include "conio.h"
#include <vector>
#include <deque>

using namespace std;

int main()
{

    char iword[200];
    string iword2, astring;
    int ilength=0, acount=0, ecount=0, icount=0, jcount=0, dcount=0;





    cout<<"Write a word to encrypt: ";
    cin.get(iword, sizeof(iword)); cin.ignore(1000, '\n');
    iword2=iword;
    ilength=iword2.length();




for(int i=0; i<ilength; i++){
    if(iword[i]=='A'){
    iword[i]='H';}

    else if(iword[i]=='B'){
    iword[i]='i';}

    else if(iword[i]=='C'){
    iword[i]='N';}

    else if(iword[i]=='D'){
    iword[i]='T';}

    else if(iword[i]=='E'){
    iword[i]='p';}

    else if(iword[i]=='F'){
    iword[i]='f';}

    else if(iword[i]=='G'){
    iword[i]='P';}

    else if(iword[i]=='H'){
    iword[i]='e';}

    else if(iword[i]=='I'){
    iword[i]='v';}

    else if(iword[i]=='J'){
    iword[i]='A';}

    else if(iword[i]=='K'){
    iword[i]='S';}

    else if(iword[i]=='L'){
    iword[i]='F';}

    else if(iword[i]=='M'){
    iword[i]='q';}

    else if(iword[i]=='N'){
    iword[i]='l';}

    else if(iword[i]=='O'){
    iword[i]='t';}

    else if(iword[i]=='P'){
    iword[i]='D';}

    else if(iword[i]=='Q'){
    iword[i]='g';}

    else if(iword[i]=='R'){
    iword[i]='f';}

    else if(iword[i]=='S'){
    iword[i]='e';}

    else if(iword[i]=='T'){
    iword[i]='B';}

    else if(iword[i]=='U'){
    iword[i]='j';}

    else if(iword[i]=='V'){  //bcdehkmnrsu EGIJKLMOQRUV
    iword[i]='C';}

    else if(iword[i]=='X'){
    iword[i]='o';}

    else if(iword[i]=='Y'){
    iword[i]='a';}

    else if(iword[i]=='Z'){
    iword[i]='f';}





}


deque<int>a_deque(100);
    for(int i=0; i<ilength; i++){
            if(iword[i]=='H'){
    acount++;
    a_deque.push_front(i);


}
    }

 for(int i=1; i<a_deque.size(); i++){
        try{cout<<"\nPlace: "<<i<<" Number: "<<a_deque.at(i)<<endl;}
        catch(exception& e){cout<<"\nElement "<<i<<" exceeds deck dimensions.";}

    }















    return 0;
}


Whats relevant though is why the deque does not store the position of the word.
Topic archived. No new replies allowed.