Thread: String Problems

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    String Problems

    One quick question: Can anyone find the error in this code, i cant seem to get the code to display a user-entered string correctly. It works with other programs but not with this one.

    Code:
    Code:
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        cout<<"(1)  Encode\n";
        cout<<"(2)  Decode\n";
        cout<<"(3)  Exit\n\n";
        int choice;
        cout<<"Choice: ";
        cin>>choice;
        if(choice==1)
        {
            //ENCODE
            char string[100];
            char enckey[100];
            char newstr[100];
            cout<<"\n\n"<<"--- ENCODER ---";
            cout<<"\n\n";
            cout<<"Enter String: ";
            cin.getline(string, 100);
            cin.ignore();
            cout<<string<<endl;
            cin.ignore();
        }
        return 0;
    }

    When it is supposed to display the string and pause, it continues and exits.

    -NOTE: This program isn't finished yet (obviously).

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    perhaps you're confusing cin.ignore() with cin.get() ?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2004
    Location
    Mercer, PA
    Posts
    25
    Try this...

    Code:
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        cout<<"(1)  Encode\n";
        cout<<"(2)  Decode\n";
        cout<<"(3)  Exit\n\n";
        int choice;
        cout<<"Choice: ";
        cin>>choice;
        if(choice==1)
        {
            //ENCODE
            char string[100];
            char enckey[100];
            char newstr[100];
            cout<<"\n\n"<<"--- ENCODER ---";
            cout<<"\n\n";
            cout<<"Enter String: ";
            cin.ignore();
            cin.getline(string, 100);
            cout<<string<<endl;
            cin.ignore();
        }
        return 0;
    }
    The problem is once you hit enter from "choice" that remains in the keyboard buffer. You need to make a call to ignore BEFORE you make a call to getline.
    If your looking into pauseing a program you could also make a call to the system function and ask for a pause - if your using windows that is - im not sure of its compatability with linux. Its better to have portable code so calling getch is probably a better idea.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    cin.get() doesnt seem to work either, the program just keeps going and it happens in some other programs of mine too (some of which run through an infinite loop

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    Question I do not understand...

    I do not understand. What are you trying to get the program to do? I could make it error free by doing the following:
    Code:
    #include <iostream>
    using namespace std;
    int main();
    {cout<<"Tada, the syntax is correct!!";
    cin.get();}
    However, that doesn't accomplish anything. What do you want the program to accomplish?

  6. #6
    Banned
    Join Date
    Oct 2004
    Posts
    250
    i think your a bit messed up
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	char string[100];
    
    	cout <<"enter something"<<endl;
    	cin.getline(string,100);
    
    	cout <<"you entered "<<string<<endl;
    
    	cin.get();
    	return 0;
    }
    this should help you do what you want to do

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    Changing your code to this works with me:

    Code:
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        cout<<"(1)  Encode\n";
        cout<<"(2)  Decode\n";
        cout<<"(3)  Exit\n\n";
        int choice;
        cout<<"Choice: ";
        cin>>choice;
    	cin.ignore();               //discard the newline from stream
    								
        if(choice==1)
        {
            //ENCODE
            char string[100];
            char enckey[100];
            char newstr[100];
            cout<<"\n\n"<<"--- ENCODER ---";
            cout<<"\n\n";
            cout<<"Enter String: ";
            cin.getline(string, 100);
            cout<<string<<endl;
        }
        cin.get();
        return 0;
    }
    You only need the cin.ignore() to discard the newline that is left in the stream when using cin>>. Else the first getline will encounter the leftover newline and take it as the input. Since getline reads and removes the delimiter newline but does not store it in, nothing changes in string. You don't need cin.ignore after getline, because - as already mentioned - getline discards the ending newline for you.
    The last cin.get() is only for keeping the console open.
    Hope this helps.

  8. #8
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Thanks so much now i can fix so many programs with this prblem you guys have been a great help!

    To the person who didn;t understand what the program accomplished:
    For one it isnt finished, i mentioned that
    2.) When it is finished it is going to be a cryptography program which encodes string

    Once again many many thanks!

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Thumbs down Correct syntax?

    Quote Originally Posted by Razorblade Kiss
    I do not understand. What are you trying to get the program to do? I could make it error free by doing the following:
    Code:
    #include <iostream>
    using namespace std;
    int main();
    {cout<<"Tada, the syntax is correct!!";
    cin.get();}
    However, that doesn't accomplish anything. What do you want the program to accomplish?
    You might want to check your syntax here, it is not correct!

    int main(); = ????????????

    completely incorrect never use ; after main

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. io stream and string class problems
    By quizteamer in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2005, 12:07 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM