Output problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Protoman

    #1

    Output problem

    My code keeps putting the input statements on the same line, instead of
    letting me input them normally; here's the code:

    int main()
    {
    cout << "Encrypt or decrypt?[1=encrypt|0=dec rypt] ";
    bool select;
    cin >> select;
    if(select==true )
    {
    string cleartext;
    string key;
    cout << "Enter the cleartext: " << endl;
    getline(cin,cle artext,'\n');
    cout << "Enter the key: " << endl;
    getline(cin,key ,'\n');
    string ciphertext(encr ypt(cleartext,k ey));
    cout << "Ciphertext : " << ciphertext << endl;
    }
    string ciphertext;
    string key;
    cout << "Enter the ciphertext: ";
    getline(cin,cip hertext,'\n');
    cout << "Enter the key: ";
    getline(cin,key ,'\n');
    string decrypted(decry pt(ciphertext,k ey));
    cout << "Cleartext: " << decrypted << endl;
    system("PAUSE") ;
    return EXIT_SUCCESS;
    }

    Can you help me? Thanks.

  • John Harrison

    #2
    Re: Output problem

    Protoman wrote:[color=blue]
    > My code keeps putting the input statements on the same line, instead of
    > letting me input them normally; here's the code:
    >
    > int main()
    > {
    > cout << "Encrypt or decrypt?[1=encrypt|0=dec rypt] ";
    > bool select;
    > cin >> select;
    > if(select==true )
    > {
    > string cleartext;
    > string key;
    > cout << "Enter the cleartext: " << endl;
    > getline(cin,cle artext,'\n');
    > cout << "Enter the key: " << endl;
    > getline(cin,key ,'\n');
    > string ciphertext(encr ypt(cleartext,k ey));
    > cout << "Ciphertext : " << ciphertext << endl;
    > }
    > string ciphertext;
    > string key;
    > cout << "Enter the ciphertext: ";
    > getline(cin,cip hertext,'\n');
    > cout << "Enter the key: ";
    > getline(cin,key ,'\n');
    > string decrypted(decry pt(ciphertext,k ey));
    > cout << "Cleartext: " << decrypted << endl;
    > system("PAUSE") ;
    > return EXIT_SUCCESS;
    > }
    >
    > Can you help me? Thanks.
    >[/color]

    I'm not completely sure I follow you but I think the problem is this

    cin >> select;

    followed by this

    getline(cin,cle artext,'\n');

    (BTW you don't need to put '\n', it is the default).

    Think carefully about what the two statements do. The first reads a
    boolean value but does not read the newline character. Even though you
    have typed a newline character (i.e. the enter key) it has not been read
    yet. Then you get to the getline statement, that reads upto the next
    newline. Well the next newline character is the one you typed after
    entering the boolean value, which is not what you want.

    The answer is to tell C++ to ignore the newline character after the
    boolean value. You do that with this strange code

    cin >> select;
    cin.ignore(INT_ MAX, '\n');
    ....
    getline(cin,cle artext);

    You need to include <limits.h> to get INT_MAX defined.

    This should be a FAQ but it doesn't seem to be.

    john

    Comment

    • Protoman

      #3
      Re: Output problem

      OK, but now it doesn't show the string "decrypted" .

      Comment

      • Neelesh

        #4
        Re: Output problem


        John Harrison wrote:[color=blue]
        > The answer is to tell C++ to ignore the newline character after the
        > boolean value. You do that with this strange code
        >
        > cin >> select;
        > cin.ignore(INT_ MAX, '\n');[/color]

        you can also use
        cin.ignore(1);
        since it is only one newline character which you want to ignore.
        [color=blue]
        > ...
        > getline(cin,cle artext);
        >
        > You need to include <limits.h> to get INT_MAX defined.
        >[/color]
        Prefer <climits>. Still better, include <limits> and use
        std::numeric_li mits<int>::max( );

        Comment

        • John Harrison

          #5
          Re: Output problem

          Protoman wrote:[color=blue]
          > OK, but now it doesn't show the string "decrypted" .
          >[/color]

          You mean that when you execute

          cout << "Cleartext: " << decrypted << endl;

          nothing appears on the screen?

          I don't know why that would be.

          This might be a case where it would help to post a complete program. You
          could replace encrypt with a dummy function (e.g.)

          string encrypt(string x, string y)
          {
          return x;
          }

          and then post all the rest of the code.

          john

          Comment

          • John Harrison

            #6
            Re: Output problem

            Neelesh wrote:[color=blue]
            > John Harrison wrote:
            >[color=green]
            >>The answer is to tell C++ to ignore the newline character after the
            >>boolean value. You do that with this strange code
            >>
            >>cin >> select;
            >>cin.ignore(IN T_MAX, '\n');[/color]
            >
            >
            > you can also use
            > cin.ignore(1);
            > since it is only one newline character which you want to ignore.
            >[/color]

            What if the user has typed a space between the boolean and the newline?
            [color=blue]
            >[color=green]
            >>...
            >>getline(cin,c leartext);
            >>
            >>You need to include <limits.h> to get INT_MAX defined.
            >>[/color]
            >
            > Prefer <climits>. Still better, include <limits> and use
            > std::numeric_li mits<int>::max( );
            >[/color]

            Why is that better?

            john

            Comment

            • Protoman

              #7
              Re: Output problem

              OK, here's the rest:

              #ifndef VIGENERE_HPP
              #define VIGENERE_HPP
              #include <iostream>
              #include <algorithm>
              #include <cstdlib>
              #include <cctype>
              #include <string>
              using namespace std;

              namespace
              {
              const char vTable[26][27]=
              {
              {'A','B','C','D ','E','F','G',' H','I','J','K', 'L','M','N','O' ,'P','Q','R','S ','T','U','V',' W','X','Y','Z', '\0'},

              {'B','C','D','E ','F','G','H',' I','J','K','L', 'M','N','O','P' ,'Q','R','S','T ','U','V','W',' X','Y','Z','A', '\0'},

              {'C','D','E','F ','G','H','I',' J','K','L','M', 'N','O','P','Q' ,'R','S','T','U ','V','W','X',' Y','Z','A','B', '\0'},

              {'D','E','F','G ','H','I','J',' K','L','M','N', 'O','P','Q','R' ,'S','T','U','V ','W','X','Y',' Z','A','B','C', '\0'},

              {'E','F','G','H ','I','J','K',' L','M','N','O', 'P','Q','R','S' ,'T','U','V','W ','X','Y','Z',' A','B','C','D', '\0'},

              {'F','G','H','I ','J','K','L',' M','N','O','P', 'Q','R','S','T' ,'U','V','W','X ','Y','Z','A',' B','C','D','E', '\0'},

              {'G','H','I','J ','K','L','M',' N','O','P','Q', 'R','S','T','U' ,'V','W','X','Y ','Z','A','B',' C','D','E','F', '\0'},

              {'H','I','J','K ','L','M','N',' O','P','Q','R', 'S','T','U','V' ,'W','X','Y','Z ','A','B','C',' D','E','F','G', '\0'},

              {'I','J','K','L ','M','N','O',' P','Q','R','S', 'T','U','V','W' ,'X','Y','Z','A ','B','C','D',' E','F','G','H', '\0'},

              {'J','K','L','M ','N','O','P',' Q','R','S','T', 'U','V','W','X' ,'Y','Z','A','B ','C','D','E',' F','G','H','I', '\0'},

              {'K','L','M','N ','O','P','Q',' R','S','T','U', 'V','W','X','Y' ,'Z','A','B','C ','D','E','F',' G','H','I','J', '\0'},

              {'L','M','N','O ','P','Q','R',' S','T','U','V', 'W','X','Y','Z' ,'A','B','C','D ','E','F','G',' H','I','J','K', '\0'},

              {'M','N','O','P ','Q','R','S',' T','U','V','W', 'X','Y','Z','A' ,'B','C','D','E ','F','G','H',' I','J','K','L', '\0'},

              {'N','O','P','Q ','R','S','T',' U','V','W','X', 'Y','Z','A','B' ,'C','D','E','F ','G','H','I',' J','K','L','M', '\0'},

              {'O','P','Q','R ','S','T','U',' V','W','X','Y', 'Z','A','B','C' ,'D','E','F','G ','H','I','J',' K','L','M','N', '\0'},

              {'P','Q','R','S ','T','U','V',' W','X','Y','Z', 'A','B','C','D' ,'E','F','G','H ','I','J','K',' L','M','N','O', '\0'},

              {'Q','R','S','T ','U','V','W',' X','Y','Z','A', 'B','C','D','E' ,'F','G','H','I ','J','K','L',' M','N','O','P', '\0'},

              {'R','S','T','U ','V','W','X',' Y','Z','A','B', 'C','D','E','F' ,'G','H','I','J ','K','L','M',' N','O','P','Q', '\0'},

              {'S','T','U','V ','W','X','Y',' Z','A','B','C', 'D','E','F','G' ,'H','I','J','K ','L','M','N',' O','P','Q','R', '\0'},

              {'T','U','V','W ','X','Y','Z',' A','B','C','D', 'E','F','G','H' ,'I','J','K','L ','M','N','O',' P','Q','R','S', '\0'},

              {'U','V','W','X ','Y','Z','A',' B','C','D','E', 'F','G','H','I' ,'J','K','L','M ','N','O','P',' Q','R','S','T', '\0'},

              {'V','W','X','Y ','Z','A','B',' C','D','E','F', 'G','H','I','J' ,'K','L','M','N ','O','P','Q',' R','S','T','U', '\0'},

              {'W','X','Y','Z ','A','B','C',' D','E','F','G', 'H','I','J','K' ,'L','M','N','O ','P','Q','R',' S','T','U','V', '\0'},

              {'X','Y','Z','A ','B','C','D',' E','F','G','H', 'I','J','K','L' ,'M','N','O','P ','Q','R','S',' T','U','V','W', '\0'},

              {'Y','Z','A','B ','C','D','E',' F','G','H','I', 'J','K','L','M' ,'N','O','P','Q ','R','S','T',' U','V','W','X', '\0'},

              {'Z','A','B','C ','D','E','F',' G','H','I','J', 'K','L','M','N' ,'O','P','Q','R ','S','T','U',' V','W','X','Y', '\0'},

              };

              string encrypt(const string& cleartext,const string& key)
              {
              string encrypted;
              for(int i=0;i<cleartext .length();i++)
              encrypted+=vTab le[cleartext[i]-'A'][key[i%key.length()]-'A'];
              return encrypted;
              }
              }

              string decrypt(const string& ciphertext, const string& key)
              {
              string decrypted;
              for (int i=0;i<ciphertex t.length();i++)
              for (char j=0;j<27;j++)
              {
              if (vTable[j][key[i%key.length()]-'A']==ciphertext[i])
              {
              decrypted+=stat ic_cast<char>(j +'A');
              break;
              }
              }
              return decrypted;
              }
              #endif

              Comment

              • Protoman

                #8
                Re: Output problem

                OK, but now it doesn't show the string "decrypted" .

                Comment

                • Protoman

                  #9
                  Re: Output problem

                  Here's the rest:

                  #ifndef VIGENERE_HPP
                  #define VIGENERE_HPP
                  #include <iostream>
                  #include <algorithm>
                  #include <cstdlib>
                  #include <cctype>
                  #include <string>
                  using namespace std;

                  namespace
                  {
                  const char vTable[26][27]=
                  {
                  {'A','B','C','D ','E','F','G',' H','I','J','K', 'L','M','N','O' ,'P','Q','R','S ','T','U','V',' W','X','Y','Z', '\0'},

                  {'B','C','D','E ','F','G','H',' I','J','K','L', 'M','N','O','P' ,'Q','R','S','T ','U','V','W',' X','Y','Z','A', '\0'},

                  {'C','D','E','F ','G','H','I',' J','K','L','M', 'N','O','P','Q' ,'R','S','T','U ','V','W','X',' Y','Z','A','B', '\0'},

                  {'D','E','F','G ','H','I','J',' K','L','M','N', 'O','P','Q','R' ,'S','T','U','V ','W','X','Y',' Z','A','B','C', '\0'},

                  {'E','F','G','H ','I','J','K',' L','M','N','O', 'P','Q','R','S' ,'T','U','V','W ','X','Y','Z',' A','B','C','D', '\0'},

                  {'F','G','H','I ','J','K','L',' M','N','O','P', 'Q','R','S','T' ,'U','V','W','X ','Y','Z','A',' B','C','D','E', '\0'},

                  {'G','H','I','J ','K','L','M',' N','O','P','Q', 'R','S','T','U' ,'V','W','X','Y ','Z','A','B',' C','D','E','F', '\0'},

                  {'H','I','J','K ','L','M','N',' O','P','Q','R', 'S','T','U','V' ,'W','X','Y','Z ','A','B','C',' D','E','F','G', '\0'},

                  {'I','J','K','L ','M','N','O',' P','Q','R','S', 'T','U','V','W' ,'X','Y','Z','A ','B','C','D',' E','F','G','H', '\0'},

                  {'J','K','L','M ','N','O','P',' Q','R','S','T', 'U','V','W','X' ,'Y','Z','A','B ','C','D','E',' F','G','H','I', '\0'},

                  {'K','L','M','N ','O','P','Q',' R','S','T','U', 'V','W','X','Y' ,'Z','A','B','C ','D','E','F',' G','H','I','J', '\0'},

                  {'L','M','N','O ','P','Q','R',' S','T','U','V', 'W','X','Y','Z' ,'A','B','C','D ','E','F','G',' H','I','J','K', '\0'},

                  {'M','N','O','P ','Q','R','S',' T','U','V','W', 'X','Y','Z','A' ,'B','C','D','E ','F','G','H',' I','J','K','L', '\0'},

                  {'N','O','P','Q ','R','S','T',' U','V','W','X', 'Y','Z','A','B' ,'C','D','E','F ','G','H','I',' J','K','L','M', '\0'},

                  {'O','P','Q','R ','S','T','U',' V','W','X','Y', 'Z','A','B','C' ,'D','E','F','G ','H','I','J',' K','L','M','N', '\0'},

                  {'P','Q','R','S ','T','U','V',' W','X','Y','Z', 'A','B','C','D' ,'E','F','G','H ','I','J','K',' L','M','N','O', '\0'},

                  {'Q','R','S','T ','U','V','W',' X','Y','Z','A', 'B','C','D','E' ,'F','G','H','I ','J','K','L',' M','N','O','P', '\0'},

                  {'R','S','T','U ','V','W','X',' Y','Z','A','B', 'C','D','E','F' ,'G','H','I','J ','K','L','M',' N','O','P','Q', '\0'},

                  {'S','T','U','V ','W','X','Y',' Z','A','B','C', 'D','E','F','G' ,'H','I','J','K ','L','M','N',' O','P','Q','R', '\0'},

                  {'T','U','V','W ','X','Y','Z',' A','B','C','D', 'E','F','G','H' ,'I','J','K','L ','M','N','O',' P','Q','R','S', '\0'},

                  {'U','V','W','X ','Y','Z','A',' B','C','D','E', 'F','G','H','I' ,'J','K','L','M ','N','O','P',' Q','R','S','T', '\0'},

                  {'V','W','X','Y ','Z','A','B',' C','D','E','F', 'G','H','I','J' ,'K','L','M','N ','O','P','Q',' R','S','T','U', '\0'},

                  {'W','X','Y','Z ','A','B','C',' D','E','F','G', 'H','I','J','K' ,'L','M','N','O ','P','Q','R',' S','T','U','V', '\0'},

                  {'X','Y','Z','A ','B','C','D',' E','F','G','H', 'I','J','K','L' ,'M','N','O','P ','Q','R','S',' T','U','V','W', '\0'},

                  {'Y','Z','A','B ','C','D','E',' F','G','H','I', 'J','K','L','M' ,'N','O','P','Q ','R','S','T',' U','V','W','X', '\0'},

                  {'Z','A','B','C ','D','E','F',' G','H','I','J', 'K','L','M','N' ,'O','P','Q','R ','S','T','U',' V','W','X','Y', '\0'},

                  };

                  string encrypt(const string& cleartext,const string& key)
                  {
                  string encrypted;
                  for(int i=0;i<cleartext .length();i++)
                  encrypted+=vTab le[cleartext[i]-'A'][key[i%key.length()]-'A'];
                  return encrypted;
                  }
                  }

                  string decrypt(const string& ciphertext, const string& key)
                  {
                  string decrypted;
                  for (int i=0;i<ciphertex t.length();i++)
                  for (char j=0;j<27;j++)
                  {
                  if (vTable[j][key[i%key.length()]-'A']==ciphertext[i])
                  {
                  decrypted+=stat ic_cast<char>(j +'A');
                  break;
                  }
                  }
                  return decrypted;
                  }
                  #endif

                  Comment

                  • Neelesh

                    #10
                    Re: Output problem

                    John Harrison wrote:[color=blue]
                    > Neelesh wrote:[color=green]
                    > > John Harrison wrote:
                    > >[color=darkred]
                    > >>The answer is to tell C++ to ignore the newline character after the
                    > >>boolean value. You do that with this strange code
                    > >>
                    > >>cin >> select;
                    > >>cin.ignore(IN T_MAX, '\n');[/color]
                    > >
                    > >
                    > > you can also use
                    > > cin.ignore(1);
                    > > since it is only one newline character which you want to ignore.[/color][/color]
                    [color=blue][color=green]
                    > >[/color]
                    >
                    > What if the user has typed a space between the boolean and the newline?[/color]

                    Yes, we will need your version in that case. I was assuming that user
                    will simply hit return after the boolean.
                    [color=blue]
                    >[color=green]
                    > >[color=darkred]
                    > >>...
                    > >>getline(cin,c leartext);
                    > >>
                    > >>You need to include <limits.h> to get INT_MAX defined.
                    > >>[/color]
                    > >
                    > > Prefer <climits>. Still better, include <limits> and use
                    > > std::numeric_li mits<int>::max( );
                    > >[/color]
                    >
                    > Why is that better?[/color]

                    I am not saying that the previous one will not work. But from what I
                    have read at various places on net and in various books is that
                    <climits> or <limits> is specifically designed for usage with C++ (for
                    defining implementation dependent limits) - may be because it puts the
                    names in a namespace and doesnot pollute the global namespace. Please
                    correct me if I am wrong.

                    Comment

                    • Neil Cerutti

                      #11
                      Re: Output problem

                      On 2005-11-11, Neelesh <neelesh.bodas@ gmail.com> wrote:[color=blue]
                      >
                      > John Harrison wrote:[color=green]
                      >> The answer is to tell C++ to ignore the newline character after the
                      >> boolean value. You do that with this strange code
                      >>
                      >> cin >> select;
                      >> cin.ignore(INT_ MAX, '\n');[/color]
                      >
                      > you can also use
                      > cin.ignore(1);
                      > since it is only one newline character which you want to ignore.
                      >[color=green]
                      >> ...
                      >> getline(cin,cle artext);
                      >>
                      >> You need to include <limits.h> to get INT_MAX defined.
                      >>[/color]
                      > Prefer <climits>. Still better, include <limits> and use
                      > std::numeric_li mits<int>::max( );[/color]

                      Or, even more specifically:

                      std::numeric_li mits<streamsize >::max();

                      --
                      Neil Cerutti

                      Comment

                      • Karl Heinz Buchegger

                        #12
                        Re: Output problem

                        Protoman wrote:[color=blue]
                        >
                        > OK, but now it doesn't show the string "decrypted" .[/color]

                        Fire up your debugger, step through the code and watch
                        as the variables change.

                        That's what the I do, what all programmers around me do, probably
                        what most programmers around the world do.

                        Should be good enough for you also :-)

                        --
                        Karl Heinz Buchegger
                        kbuchegg@gascad .at

                        Comment

                        • John Harrison

                          #13
                          Re: Output problem

                          Protoman wrote:[color=blue]
                          > Here's the rest:
                          >[/color]

                          I ran your code. It worked for me provided I remembered to enter
                          uppercase text.

                          john

                          Comment

                          • Mike Wahler

                            #14
                            Re: Output problem


                            "Neelesh" <neelesh.bodas@ gmail.com> wrote in message
                            news:1131699928 .586887.159060@ o13g2000cwo.goo glegroups.com.. .
                            [color=blue]
                            > I was assuming that user[/color]

                            Such an attitude is the source of uncountable bugs.

                            -Mike


                            Comment

                            • Howard

                              #15
                              Re: Output problem


                              "Protoman" <Protoman2050@g mail.com> wrote in message
                              news:1131697014 .799837.315880@ o13g2000cwo.goo glegroups.com.. .[color=blue]
                              > OK, but now it doesn't show the string "decrypted" .
                              >[/color]

                              Still haven't learned to quote what you're responding to?

                              -Howard


                              Comment

                              Working...