Goto Skip Problem

Pages: 12
Hello there, i have this code and at the part with encrypt (goto) it cout first line and skipp all and return, i dont know how to fix this, can ya help me? Thanks in advance!

For more exact info, compile and run it, after chose/type encrypt and you can see my problem, i hope i will fix it!

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
#include <iostream>
#include <string>
#include "encry loop.h"
#include <stdlib.h>

int a = 0;
std::string w;
char b = w.length();

int main()
{
    std::cout<<"What to you want to do?"<<endl;
    std::string k;
    std::cin >> k;
    if(k=="Encrypt" || k=="encrypt")
    {
        system("cls");
        goto encrypt;
    }

    if(k=="Decrypt" || k=="decrypt")
    {
        system("cls");
        goto decrypt;
    }

    return 0;

    encrypt:
    std::cout << "Sentence to be encrypted: "<<endl<<endl;
    std::getline (std::cin,w);

    cout<<" "<<endl;
    for (int i = 0; i < b; i++)
    {
    char c = w[a];
    eloop(c);
    a = a + 1;
    }

    cout<<" "<<endl<<endl;
    return 0;

    decrypt:
    std::cout<<"Decrypt is coming soon!"<<endl;

    return 0;
}
closed account (48T7M4Gy)
replace it with a function call and corresponding function is one possibility.
Formatted vs Unformatted I/O
You are mixing formatted and unformatted input, which is causing your problem.

On line 14 you use formatted input to get a space-delimited string from the user. This leaves the newline (Enter key) in the input.

Then on line 31 you ask for unformatted input, but that newline (Enter key) is still waiting to be read, so it is and that's what you get.

To see better what is happening, try entering something like "encrypt my dear aunt sally" and see what happens.

Goto
Why are you using goto?

Either use a function, or perform the proper thing in the if..else branches:

15
16
17
18
19
20
21
22
    if(k=="Encrypt" || k=="encrypt")
    {
        ask_for_and_encrypt_user_input();
    }
    if(k=="Decrypt" || k=="decrypt")
    {
        ask_for_and_decrypt_user_input();
    }
15
16
17
18
19
20
21
22
23
24
25
    if(k=="Encrypt" || k=="encrypt")
    {
        std::string string_to_encrypt;
        std::cout << "Sentence to be encrypted: \n\n";
        std::getline(std::cin, string_to_encrypt);
        ...
    }
    if(k=="Decrypt" || k=="decrypt")
    {
        ...
    }

Hope this helps.
Could you please show us what your function eloop() is?
eloop() is another file with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "encry loop.h"
#include <string>

using namespace std;

void eloop(char c)
{
    //letters

    if(c=='a' || c=='A')
{
cout<<"00100"<<endl;
}
    if(c=='b'||c=='B')
{
...
}
......
......


I try now and i will tell you back if it worked! thanks for answering!
Last edited on
Hello, im back, there is the same problem but now i have this code:
*I still not understand how to fix it*

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
#include <iostream>
#include <string>
#include "encry loop.h"

int a = 0;
std::string w;
char b = w.length();

int main()
{
    std::cout<<"What to you want to do?"<<endl;
    std::string k;
    std::cin >> k;
    if(k=="Encrypt" || k=="encrypt")
    {
        std::cout << "Sentence to be encrypted: "<<endl<<endl;
        std::getline (std::cin,w);

        std::cout<<" "<<endl;
        for (int i = 0; i < b; i++)
        {
            char c = w[a];
            eloop(c);
            a = a + 1;
        }

        cout<<" "<<endl<<endl;
        return 0;
    }

    if(k=="Decrypt" || k=="decrypt")
    {
        std::cout<<"Decrypt is coming soon!"<<endl;
        return 0;
    }

    return 0;
}
Last edited on
What exactly is your problem? What do you want to achieve?
closed account (48T7M4Gy)
Have you made sure your .h file and this file are in the same project directory or linked appropriately? (in Xcode this is not often straightforward)

Also the "encry loop.h" file name shouldn't have a space.
You didn't measure the length of w after inputting.
std::getline (std::cin,w); b = w.length();
+closed account 5a8Ym39o6
http://puu.sh/pSbGi/12ceefd491.png
There, where you see at my program "Sentence to be encrypted".
It needed to be:
*Insert text* (Random text)
*Show text* (something like 01012 00101 ...)

But it give white space and skip the insert (and after) part and go to the return.
I hope i explained good

+kemort
Im sure, look at the image.

+closed account 5a8Ym39o6
I change it, same problem.
closed account (48T7M4Gy)
Cosmin, You're using codeblocks, not XCode.

If you have a mac then you are better off with xcode if you can.
Last edited on
The program did tell you to input something but you didn't seem to be inputting it and simply pressing Enter. Should it be "Hello World", "Homeland", etc

If the function getline happens not to work, use cin >> w; instead.
Use this :
std::cout << "Enter sentence to be encrypted : ";
+kemort
Yeah, is this a bad thing?

Also, there's a fix for my problem? :(
I found the problem,
at std::getline (std::cin,w); if i change it with cin >> w; it will solve the problem, but after, it will not take the complete sentence, only first word.
closed account (48T7M4Gy)
Yeah, is this a bad thing?

No, if you like CodeBlocks its OK. It works.

Also, there's a fix for my problem?
Which problem? There are a few subplots going on here.
Then you use this :
1
2
3
char tmp[1024];
cin.getline(tmp, 1024, '\n');
w = tmp;
Last edited on
closed account (48T7M4Gy)
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //prepare for following getline() after a cin

See the following if this is the problem you're talking about and you want a good long explanation http://stackoverflow.com/questions/11835226/program-skips-cin-getline
Last edited on
i writed cin.ignore(); before std::getline (std::cin,w); and it solved my problem, thank you guys so much!
Last edited on
Glad, this makes me happy too :)
Pages: 12