Program skipping cin's

closed account (Ey6Cko23)
I am writing this program and it worked fine but since i added the selector it skips over the cin's in he input function. this used to work fine
here is 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
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
#include "stdafx.h" //needed for reasons
#include <iostream>
void input(); //forward declarations so the compiler doesn't cry
//void varToInt();
void key();
int encrypting();
void wait();
void choice();
int decrypting();

char toEncrypt[140]; //declaring the global vars and arrays
char keyVar[10];
int choiceVar;


int main()
{
    choice();
    wait();
    return 0;
}


void choice()
{
    std::cout << "1 = encrypt, 2 = decrypt \nPlease choose encrypt or decrypt:";
    std::cin >> choiceVar;
    std::cout << "\n";
    if (choiceVar == 2)
    {
        input();
        key();
        decrypting();
    }
    if (choiceVar == 1)
    {
        input();
        key();
        encrypting();
    }
}


void input()
{
    std::cout << "*MAX 140 CHARACTERS* \n Please input a word/sentence to encrypt: ";
    std::cin.getline(toEncrypt, 140);
    std::cout << "Input all good \n";
}

//void varToInt()
//{
//    int encrypted[140];
//    encrypted[1] = toEncrypt;
//}

void key()
{
    std::cout << "Enter key: ";
    std::cin >> keyVar;
}

int decrypting()
{
    for (int i = 0; toEncrypt[i] != 0; ++i)
    {
        toEncrypt[i] -= 1;
    }
    std::cout << "Encrypted: " << toEncrypt;
    return 0;
}

int encrypting()
{
    for (int i = 0; toEncrypt[i] != 0; ++i)
    {
        toEncrypt[i] += 1;
    }
    std::cout << "Encrypted: " << toEncrypt;
    return 0;
}

void wait()
{
    std::cout << "Press Enter to continue...";
    std::cin.clear(); //makes sure the program doesnt directly close when finished
    std::cin.ignore(32767, '\n');
    std::cin.get();
    
}
Last edited on
After you read input on line 27 with >>, you leave whitespace in the buffer including the newline (enter you pressed). Then when you try to read with getline(), it reads the enter press and gives you the empty string. You can use ignore() to ignore up to that newline after you read formatted input.
closed account (Ey6Cko23)
thank you
Topic archived. No new replies allowed.