Unsized string input

This is a discussion on Unsized string input within the C++ Programming forums, part of the General Programming Boards category; Hi (btw, I'm new in this Forum) I'm doing a project and I have a problem with strings and char ...

  1. #1
    DsH
    DsH is offline
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    Unsized string input

    Hi (btw, I'm new in this Forum)

    I'm doing a project and I have a problem with strings and char arrays (mainly because of the limited size, but also some other stuff).
    I'm not sure if I need to put here the code I've done, since it's mainly small stuff, but I'll do it if you find the need.

    In the program I need to open a text file chosen by the user, so I put an ifstream. Problem is: I'd like the user to be able to input an unlimited char array by a simple

    cout<< "Path to file: ";
    cin>> Path;

    or something like this...
    I tried with strings (getline) and then converting it to a char array (cause ifstream only accepts the path in char array), but for some reason the program stopped once I got to the getline part (I used "getline (cin, Filename)"). I searched for the reason, couldn't find it, tried another way.
    Right now I just have:

    char Path[200]

    cout<< "Path to file: ";
    cin>> Path;

    but it will only read until it finds a " " (if I'm not wrong).

    Later, there's another problem, but it's similar so, once I find a solution for that one, the other one is probably solved.

    (In the project, I'm going to take the text from the text file, put it to a string and then pass it to a linked list... any suggestions about that would be greatly apreciated.
    Also, I'm on my first year with Programming, so please don't get too complicated.)

    Thanks in advance.
    Last edited by DsH; 07-13-2009 at 03:53 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Code:
    std::string Filename;
    std::getline(std::cin, Filename);
    std::istream File(Filename.c_str());
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    DsH
    DsH is offline
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    That's what I tried, but it skips the input part.
    This is what I have.
    Sorry, the variables are in portuguese... But you're programmers, I think you'll get it

    Code:
            int Opcao;
    
    	do
    	{
    		cout<< "Pretende criar um ficheiro novo ou abrir um guardado? (1 - Criar Novo / 2 - Abrir Guardado): ";
    		cin>> Opcao;
    	}while(Opcao!=1 && Opcao!=2);
    
    	if(Opcao==1)
    	{
    		char Texto[2000];        //<------------ I wanted this to be an unlimited char array.... but this will do for now. I'll change it later.
    
    		cout<< "Escreva o texto: " <<endl;
    		cin>> Texto;
    
    
    	}else
    	{
    		string Caminho;
    
    		cout<< "Escreva o caminho para o ficheiro: ";
    		getline(cin, Caminho);                                     //<------- It skips this part, so I dont even have a chance to input anything.
    
    		ifstream Ficheiro(Caminho.c_str());
    
                    if(!Ficheiro.fail())
    		{
    			cout<< "Falha ao abrir o ficheiro." <<endl;
    			cout<< "Verifique se escreveu correctamente o caminho." <<endl;
    			cout<< "Exemplo: C:\user\folder\texto.txt" <<endl;
    		}
    		else
    		{
    			while (!Ficheiro.eof())
    			{
    				cout<< "ahh!"; //(just for testing)
    			}
    			Ficheiro.close();
    		}
    	}
    So the main problem seems to be on the getline, that just stops/skips... I'm working with VC++ 2008, btw.
    Last edited by DsH; 07-13-2009 at 04:51 AM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you use both cin >> and getline for input, then before the calls to getline you'll need to clear the input buffer. Otherwise it will just read the '\n' left there by cin >> and continue.

    See the C++ implementation of myflush here.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Also put your code inside [code]Code tags[/code].
    Otherwise code will be unreadable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    DsH
    DsH is offline
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    ok, I added a cin.ingore() before the getline and it's now working
    thanks

    EDIT:
    just one small thing:

    Code:
    		string Caminho;
    
    		cout<< "Escreva o caminho para o ficheiro: ";
    
    		cin.ignore();
    		getline(cin, Caminho);
    
    		ifstream Ficheiro(Caminho.c_str());
    now I have this thing (rest is still the same)
    now I tested it, anything I write as the path (even something like "sjkfbaf") will show as OK... meaning I get a full page of "ahh!"
    do you know why this happens? :/
    (it was working kinda fine just a while ago, it showed the "error case" and when i wrote the path right, it showed the "fine case")
    Last edited by DsH; 07-13-2009 at 05:34 AM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> If you use both cin >> and getline for input, then before the calls to getline you'll need to clear the input buffer.

    I prefer to state this as, if you use both cin >> and getline for input, then after calls to cin >> you'll need to ignore the trailing newline.

    The reason is that it is the trailing newline after calls to cin >> that cause the problem. If you call getline twice in a row, then the second call shouldn't have an ignore() before it or it will ignore the first character in the filename. If you always put the cin.ignore() after calls to cin >>, then you don't have that problem (and it is safe to do so even if you never use getline).

    >> just one small thing:

    I'd expect invalid paths to show the full page of "ahh!" Are you saying it doesn't work when you give it a proper path? Does it work if you hardcode the proper path into the program? (Make sure you use \\ or / instead of \ in your paths inside the code.)

    What is the correct path that isn't working?

  8. #8
    DsH
    DsH is offline
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    (I was using \\ but didnt know you could use just /)

    Ah...
    My mistake...
    Code:
    if(!Ficheiro.fail())
    was suposed to be
    Code:
    if(Ficheiro.fail())
    My fault... now it's working properly, thank you all

    (now I'm gonna bury myself in the rest of the project or suicide in a desperate atempt of avoiding the consequences of not doing it )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Replies: 14
    Last Post: 01-18-2008, 03:14 PM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM
  5. Basic C Programming Help Needed
    By Smurphygirlnz in forum C Programming
    Replies: 8
    Last Post: 09-26-2002, 07:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21