Can't find the error

Dec 2, 2013 at 3:58pm
I'm writing this code just for fun, and to better understand what I'm learning.
But when I build this code, 4 errors keep coming, but I don't know what they mean, or how to fix them..
Here's the code:

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
#include <iostream>
#include <vector>

#include "vectors.h" // Vector functions

using namespace std;

int main()
{
    cout << "MENU"           << endl
         << "[A] - average"  << endl
         << "[P] - primes"   << endl
         << "[V] - vector"   << endl
         << "[Q] - quit"     << endl;

    bool done = false;
    do
    {
        char choice;
        cin >> choice;
        cout << endl << endl;

        switch (choice){
            case 'a':
            case 'A':
                average();
                break;
            case 'p':
            case 'P':
                printPrimes();
                break;
            case 'v':
            case 'V':
                vector<char> vec(10, 5);
                print_vector_char(vec); // Prints out the vector
                cout << endl;
                fill_vector_char(vec); // Clears vector, and lets the user refill it
                print_vector_char(vec);
                break;
            case 'q':
            case 'Q':
                done = true;
                break;
        }
    } while (!done);
}


The errors are the following:

Line      Message
40        error: jump to case label [-fpermissive]
34        error: crosses initialization of 'std::vector<char> vec'
41        error: jump to case label [-fpermissive]
34        error: crosses initialization of 'std::vector<char> vec'


I really can't figure it out...
please tell me what they mean, and how to fix them :)
thanks in advance!
Dec 2, 2013 at 4:05pm
They mean you have case conditions without break; statements in them. You can avoid having to declare both cases by using the toupper function from the cctype header one your 'choice' variable anytime before the switch statement EDIT: and after the users input is done: http://www.cplusplus.com/reference/cctype/toupper/?kw=toupper

This will ensure that the input is in upper case format.
Last edited on Dec 2, 2013 at 4:06pm
Dec 2, 2013 at 4:07pm
put braces around the whole cases spanning more than one line.

See:
http://stackoverflow.com/questions/14669457/initialization-of-element-is-skipped-by-case-label
Last edited on Dec 2, 2013 at 4:12pm
Dec 2, 2013 at 4:14pm
They mean you have case conditions without break; statements in them.

Incorrect. The way he's written the cases and the breaks is just fine.
Last edited on Dec 2, 2013 at 4:27pm
Dec 2, 2013 at 4:31pm
Thanks both!

@mutexe
It did the job, but 3 new errors occured..


Line       Message
35         undefined reference to `print_vector_char(std::vector<char, std::allocator<char> >)'|
37         undefined reference to `fill_vector_char(std::vector<char, std::allocator<char> >)'|
38         undefined reference to `print_vector_char(std::vector<char, std::allocator<char> >)'|

so it didn't entirely fix it...

@Computergeek01
Neither did your solution fix it
I did this:

 
        switch (toupper(choice)){

and included the header file.
But the same errors occured on line 41 and 34..

[edit] I have to go, dinner.. Will be back in 20 minutes or so.
Last edited on Dec 2, 2013 at 4:32pm
Dec 2, 2013 at 4:32pm
You're supposed to delete the lower case conditions. The "toupper()" function makes them redundant anyway.
Dec 2, 2013 at 4:40pm
It did the job, but 3 new errors occured..


i presume these methods live in your vectors.h? how are they declared in there?
Dec 2, 2013 at 5:20pm
@Computergeek01
I did delete the lower case conditions, forgot to say that :)

@mutexe

Oh, didn't think of that..

Here's the header file:
1
2
3
4
5
6
7
8
9
10
11
// header file vectors.h
#include <vector>

using namespace std;


void print_vector_char(vector<char>);

vector<char> create_vector_char();

void fill_vector_char(vector<char>);


And the source file:
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
#include <vector>
#include <iostream>
#include <stdio.h>
#include "vectors.h"

using namespace std;

void print_vector_char(vector<char>& vec)
{
    for (unsigned i = 0; i < vec.size(); i++){
        cout << vec[i] << " ";
    }
    cout << endl;
}

void fill_vector_char(vector<char>& vec)
{
    vec.clear();
    char elem;
    do{
        cin >> elem;
        cin.ignore();                    
        vec.push_back(elem);
    } while (elem != '.');
}


Now that I think about it, the 'cin.ignore();' on line 22 is supposed to be after the 'vec.push_back(elem)'?

btw thanks for your time! :D hope you still are looking on this thread
Dec 2, 2013 at 6:31pm
Please re-paste your code if you don't mind. Those errors should have gone away with the case statements if you did it correctly.
Dec 3, 2013 at 9:01am
computergeek, no offence but your advice is not correct again. It has nothing to do with the case statements.

OP: hint, your cpp implementation has for example:
1
2
3
4
void print_vector_char(vector<char>& vec)
{
....
}


Yet your declaration of the 'same' function in your header is:
 
void print_vector_char(vector<char>);


can you see the difference?
Topic archived. No new replies allowed.