problem with reading data

Hello, I started programing an application in order to send sms through a cell phone connected via USB port, but I get this wierd problem I can't read info, "cin" is not executed exept the first one
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
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <fstream>


using namespace std;


int main()
{
     HANDLE hComm;
     char port_name[5];
     char msg[300];

    cout<<"Modem port:    ";
    cin>>port_name;

hComm = CreateFile( port_name,
                    GENERIC_READ | GENERIC_WRITE,
                    0,
                    0,
                    OPEN_EXISTING,
                    FILE_FLAG_OVERLAPPED,
                    0);
if (hComm == INVALID_HANDLE_VALUE)
   // error opening port; abort
        cout<<"ERROR opening port "<<endl;
    else
        cout<<"SUCCESS"<<endl;

cout<<"Votre message:   "; fgets(msg,300,stdin);
cout<<msg;

cout<<"choisir la liste des numeros:   ";
// le constructeur de ifstream permet d'ouvrir un fichier en lecture
std::ifstream fichier( "fichier.txt" );

    if ( fichier ) // ce test échoue si le fichier n'est pas ouvert
    {
        std::string ligne; // variable contenant chaque ligne lue

        // cette boucle s'arrête dès qu'une erreur de lecture survient
        while ( std::getline( fichier, ligne ) )
        {
            // afficher la ligne à l'écran
            std::cout << ligne << std::endl;
        }
    }

    return 0;
}
Last edited on
Try using cin.ignore(); immediately after cin on char arrays. That usually works for me. I was frustrated enough with char arrays that now I only use std::string. std::string::c_str grabs the char array from std::string.
Last edited on
the problem is with
cout<<"Votre message: ";

fgets(msg,300,stdin);
cout<<msg;
I have almost certainly always had trouble when swapping between old C input and newer C++ input.
Try using std::istream::get instead.
http://www.cplusplus.com/reference/istream/istream/get/
Topic archived. No new replies allowed.