I am having trouble reading and writing files

This is a basic c++ project I am working on. It is supposed to be a message coder/decoder; I want to take a string and translate it to ansi values or take a block of values and translate it to a sentence. I am making progress but have some hangups.

My input string to turn into code stops at a whitespace. i have tried noskipws but am sure i ddin't use it correctly. i would love for the translation to include the whitespace ansi code '32'...i think

Then my output is only grabbing the first integer entered; also I would love to have the user enter the integers without having in a 'string', I mean so they wouldn't have to press enter every time.

Thanks for taking a look, any help or even pointing in a direction is appreciated!

P.S. I used the integer 0000 as an escape value for lack of a better idea

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
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main ()
{
    int source;
    
    cout<<" If you have a message to encode enter '1'"<<endl;
    cout<<" If you have one to decode enter '2'"<<endl;
    cin>>source;
    
        switch (source){
            case 1:{
              ofstream code;
              char coder;
              string message;
              cout<<" What phrase do you want coded?"<<endl;
              cout<<" ";cin>>message;

              code.open("code.txt");
                for (size_t i=0; i < message.length(); i++){
                  coder=message.at(i);
                  code<<(int)coder;
                  code<<" ";
                }

                  code.close();

                  
          break;  }
            case 2:{
  
              ofstream code;
              int coder;
              int message;
              cout<<" What code do you want decoded?"<<endl;
              cout<<" Press enter after each integer"<<endl;
              cout<<" Enter 0000 when the message is done"<<endl;
              do{
              cout<<" ";cin>>message;
              code.open("code.txt");
                  code<<(char)message;
                  code<<" ";
                }while(message!=0000);

                  code.close();

                 
                  break;
               
            }
                break;
        }

      ifstream myReadFile;
             myReadFile.open("code.txt");
             char output[100];
                if (myReadFile.is_open()) {
                        while (!myReadFile.eof()) {
                                myReadFile.read (output, 100);
                                cout<<output;
                        }
                }
                myReadFile.close();

  return 0;
}

> My input string to turn into code stops at a whitespace.

std::getline() http://www.cplusplus.com/reference/string/getline/

1
2
std::string message ;
std::getline( std::cin >> std::skipws, message ) ;


> Then my output is only grabbing the first integer entered;
> also I would love to have the user enter the integers without having in a 'string',
> I mean so they wouldn't have to press enter every time.

1
2
int i, j, k ;
std::cin >> std::skipws >> i >> j >> k ;


Thank you that was helpful.

So i have the whitespaces working great, now something strange.

Everything works like I want it to until I add the IF statement; then it breaks and returns 0;
If I comment out the IF it works again. I tried a switch instead and got the same result as an IF

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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main () {
   char menu;
   cin>>menu;
     
        if(menu=='a'){    
        string message;
              ofstream code;
              char coder;
              cout << "Please enter message to code";
              getline (cin,message);
                          code.open("code.txt");
                            for (size_t i=0; i < message.length(); i++){
                              coder=message.at(i);
                              code<<(int)coder;
                              code<<" ";
                            }
                          code.close();
       }
        else{
                ofstream code;
                int one;
                code.open("code.txt");
                cout<<" Enter integers to decode. Press enter after each one\n"
                        " Enter 000 when done"<<endl;
                        while(one!=000){
                                cin>>one;
                                code<<(char)one;
      
                        }           
                code.close();
    
            }        
           return 0;
}
line 8: cin>>menu; This will just read one char and the new line char will still be in the input buffer so when you later call getline it will read the rest of the line which is empty unless the user had entered more characters on the same line. To fix this you can use ignore to discard the rest of the line: cin.ignore(numeric_limits<streamsize>::max(), '\n' );

On line 26 one is uninitialized when you use it on line 30 so you can't be sure what value it has. Also note that 000 is the same as 0. A better way to write the loop is
1
2
3
while(cin>>one && one != 0){
	code<<(char)one;
}

This also has the advantage that it will stop if the user enters invalid input or EOF is reached.
Thanks, I understand now.
and the initial problem is solved
but now when I output to my file it is still ignoring everything but the first character



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
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    int menu;
    
    cin>>menu;
    cin.ignore(256, '\n' );
    if(menu==1){
       
 string message;
 ofstream code;
              char coder;
              cout << "Please enter full name: ";
              
              getline (cin, message);
              
              
              
                          code.open("code.txt");
                            for (size_t i=0; i < message.length(); i++){
                              coder=message.at(i);
                              code<<(int)coder;
                              code<<" ";

                           break; }
                          code.close();}
    return 0;
}

Last edited on
Topic archived. No new replies allowed.