I can't seem to use the 'cin.get' statement for getting character arrays. I've just switched to VC++ Express, from Dev-Cpp, which allowed that. I have tried other methods like declaring the variable with 'string' but the compiler doesn't recognise it even with the <string> header.

void encrypt()
{
    string input;        // Error Given: " 'string' : undeclared identifier "
    //char input;

    char activeChar=0;
    int done=0;
    
    std::cout << "enter text (max 64 characters):\n";

    //getline (cin, input);    // Error Given: " 'getline': identifier not found "
    cin.get (input,65);        // Error Given: " 'cin' : undeclared identifier "
    
    
    do
    {
        input[activeChar]+=1;
        activeChar++;
        
        if(input[activeChar]=='\0')
            {done=1;}
            
            else
            {
                if(input[activeChar]==32)
                {input[activeChar]=31;}
            }

    } while (!done);
    
    std::cout << "encrypted: " << input << '\n' << '\n';
}

Basically, I need an alternate to 'cin.get' that uses the 'std::' format, or something else that this compiler will understand.

Recommended Answers

All 9 Replies

Try this.

#include <iostream>
#include <string>
using namespace std;

This should remove the error in first line and
"//getline (cin, input); // Error Given: " 'getline': identifier not found "

I dont think functioin get is declared in string or iostream. Thats y its giving undeclared identifier error.

Adding using namespace std; fixed it. Thanks.

Double Post :)
I'm getting a problem from the debugger now:

Program: c:\vc\projects\cryptography\debug\cryptography.exe
File .\xstring
Line: 1508
Expression: string subscript out of range

This pops up as soon as the string is declared, i've googled the error message, but the code they're using is so complex i cant make sense of the solutions. Here's the rest of the source code if needed:

/* compiler directives */
#include <iostream>
#include <string>
using namespace std;

/* Prototype functions */
void encrypt();
void decrypt();

int main()        // Main Function
{
    int exit=0;
    std::cout << "Simple Encryption" << '\n' << '\n';

    while(!exit)    // loop through the menu while 'exit' is false
    {
        int choice;

        std::cout << "1. Encrypt Text" << '\n'
                  << "2. Decrypt Text" << '\n'
                  << "0. Exit Program" << '\n';

        std::cin >> choice;

        switch (choice)
        {
        case 1:
            encrypt();
            break;
        case 2:
            decrypt();
            break;
        case 0:
            exit=1;
            break;
        default:
            choice=0;
            exit=1;
        }    //end of switch structure
    }    // end of while loop

    std::cout << "Exiting..";
    return 0;

}    // end of main function


/* encryption function */
void encrypt()
{
    string input;
    char activeChar=0;
    int done=0;
    
    std::cout << "enter text (max 64 characters):\n";

    getline(cin,input);
    
    do
    {
        input[activeChar]+=1;    // Increase the decimal value of the character by 1
        activeChar++;            // Move to next character
        
        if(input[activeChar]=='\0')    // if character is null, end loop
            {done=1;}
            
            else    // using this to keep spaces intact.
            {
                if(input[activeChar]==32)
                {input[activeChar]=31;}
            }

    } while (!done);
    
    std::cout << "encrypted: " << input << '\n' << '\n';
}

/* decryption function */
void decrypt()
{
    string input;
    char activeChar=0;
    int done=0;
    
    std::cout << "enter text (max 64 characters):\n";
    getline(cin, input);
    
    do
    {
        input[activeChar]-=1;   // Increase the decimal value of the character by 1
        activeChar++;           // move to next character
        
        if(input[activeChar]=='\0') // if character is null, end loop
        {done=1;}
            
        else
        {
            if(input[activeChar]==32)
            {input[activeChar]=31;}
        }
            
    } while (!done);
    
    std::cout << "decrypted: " << input << '\n' << '\n';
}

I am also getting the memory access error when i ran ur prgrm.
I think the problem is with the way in which u have declarec the string "input".
For debugging purpose i have made a small change in the programm and tried as shoen below. It worked.

void encrypt()
{
    char* input = new char[100];
 strcpy(input,"Hello");
    char activeChar=0;
    int done=0;
    
    std::cout << "enter text (max 64 characters):\n";
    //getline(cin,(string)input);

This is the only change I hav emade. Now the encrypt fun is working.
Please consult with somebody else also. I am quite not sure how the string class i implemented internally.

After playing around with 'cin.ignore', i got it working. Thanks for your help.

Waht does 'cin.ignore' do???

I'm not sure. I THOUGHT i knew what it did but, i don't know anymore. All i know is it fixed everything along with 'std::getline'. Huzzah!

// Simple Encryption
// Version: 0.01
// Jamal Johnson
// Last Updated: 7/04/06 @ 6:40AM.

/* compiler directives */
#include <iostream>
#include <string>
using namespace std;

/* Prototype functions */
void encrypt();
void decrypt();

int main()        // Main Function
{
    int exit=0;
    std::cout << "Simple Encryption" << '\n' << '\n';

    while(!exit)    // loop through the menu while 'exit' is false
    {
        int choice;

        std::cout << "1. Encrypt Text" << '\n'
                  << "2. Decrypt Text" << '\n'
                  << "0. Exit Program" << '\n';

        std::cin >> choice;
        cin.ignore(1,'\n');

        switch (choice)
        {
        case 1:
            encrypt();
            break;
        case 2:
            decrypt();
            break;
        case 0:
            exit=1;
            break;
        default:
            choice=0;
            exit=1;
        }    //end of switch structure
    }    // end of while loop

    std::cout << "Exiting..";
    return 0;

}    // end of main function


/* encryption function */
void encrypt()
{
    std::string input;
    char activeChar=0;
    int done=0;

    std::cout << "enter text:\n";
    std::getline(std::cin, input);
    cin.ignore(0,'\n');
    
    while(!done)
    {
        /* special checks */
        if(input[activeChar]=='\0')    // if character is null, end loop
        {done=1;}

        if(input[activeChar]==32)    // if character is a space, move to next character and skip loop
        {
        activeChar++;
        continue;
        }

        /* encryption process */
        input[activeChar]+=1;    // Increase the decimal value of the character by 1
        activeChar++;            // Move to next character
    }
    
    std::cout << "encrypted:" << input << '\n' << '\n';
}

/* decryption function */
void decrypt()
{
    std::string input;
    char activeChar=0;
    int done=0;
    
    std::cout << "enter text:\n";
    std::getline(std::cin, input);
    cin.ignore(0,'\n');
    
    while(!done)
    {
        /* special checks */
        if(input[activeChar]==32)    // if character is a space, move to next character and skip loop
        {
        activeChar++;
        continue;
        }

        if(input[activeChar]=='\0') // if character is null, end loop
        {done=1;} 

        /* decryption process */
        input[activeChar]-=1;   // Increase the decimal value of the character by 1
        activeChar++;           // move to next character

    }
    
    std::cout << "decrypted:" << input << '\n' << '\n';
}

I have modified ur encrypt as shoen below. I dont think there is any need for "ignore".

void encrypt()
{
    std::string input;
    char activeChar=0;
    int done=0;
    std::cout << "enter text:\n";
    std::getline(std::cin, input);
        
    while(!done)
    {
        /* special checks */
        if(input[activeChar]=='\0')    // if character is null, end loop
        {done=1;
    input[activeChar]= '\0';
    break;
  }
        if(input[activeChar]==32)    // if character is a space, move to next character and skip loop
        {
        activeChar++;
        continue;
        }
        /* encryption process */
        input[activeChar]+=1;    // Increase the decimal value of the character by 1
        activeChar++;            // Move to next character
    }
    
    std::cout << "encrypted:" << input << '\n' << '\n';
}

Use code tags when posting code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.