Cin >> string, ss<<a<<string //the string is giving only 1 word, why?

Hi forum, i have an problem and a question:

The question, there's any funtion to put number to how much file's there in a folder ---See line 44---

The problem, if i said the "n" is Spag Heddy - It's me.mp4 it took only the word "Spag", why? I have to say the "n" its a string ---See line 53---

Thanks in advance!

*Sorry for my english*
*This is not an homework!*
*Im beginner in c++"

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
  #if defined (_UNICODE) || defined (UNICODE)
#error "Unicode not supported - use Multi-Byte Character Set"
#endif

#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <sstream>

using namespace std;
string n, f1, flf, f2;
stringstream ss;
string InitialDir;

int main (void)
{
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;

    flf = "*.*";

    cout<<"Enter file's location: "<<endl;
    cin >> f1;

    if (f1 == "music")
    {
        f1 = "C:\\Users\\cosmi\\Desktop\\Music\\";
    }

    InitialDir =f1 + flf;

    printf ("Initial directory: %s", InitialDir.c_str());
    hFind = FindFirstFileA (InitialDir.c_str(), &FindFileData);
    if (hFind == INVALID_HANDLE_VALUE)
    {
        printf ("Error: %lu ", GetLastError ());
        system ("pause");
        return EXIT_FAILURE;
    }
    do
    {
        printf ("\nFile: %s", FindFileData.cFileName); //If theres more files, i dont know how to make looking like: File 1: document.dox File 2: notes.txt ...

    } while (FindNextFileA (hFind, &FindFileData));

    FindClose (hFind);

    cout<<" \n \nWhich you want to run?"<<endl;
    cin >> n;

    ss << "start " << f1 << n; //There's the problem, if i say n its "Spag Heddy - It's me.mp4" it will took only "Spag", why?
    system (ss.str().c_str());

    return 0;

    return EXIT_SUCCESS;
}
cin>> reads input as far as the first space. If you want to enter a string containing spaces, use getline.

When you do that, be aware that cin>> will leave a '\n' in the buffer, so if you're going to use getline after using cin>>, clear the buffer.
but how to use it, you can give me an example? thanks in advance
I still dont know how to fix my problem with getline, please help me :/
Last edited on
cin >> n; std::getline (std::cin,n);
Its not working, its something like its skiping the cin and go directly to start command, so its not working. I hope i will fix it :/
if you're going to use getline after using cin>>, clear the buffer.

Here's one way.
change
 
    std::cin >> f1;
to
 
    std::cin >> f1 >> std::ws;

std::ws will read and discard whitespace - in particular the newline '\n' remaining after entering the value of f1.
http://www.cplusplus.com/reference/istream/ws/
Still dont work, The system cannot find the file Spag, HOW TO FIX IT????
You need to combine the answers given by Moschops with the one I gave. Mine was only half a solution, the part needed to get the following getline to work.
cin >>n;
1
2
std::getline (std::cin,n);
         std::cin >> f1 >> std::ws;


Like this? (this is not working)
Anyway, i fix m problem, thanks for trying too!
Topic archived. No new replies allowed.