Can someone let me know how to use the .peek() and .get() together in this code?

So I have to use peek and get in this code to see if the next character is a whitespace. While there is a white space, it runs true. As long as it is a white space it will keep grabbing the white space until there is no longer a white space. Once there is no white space, it ends the loop and returns the collected characters into a vector of strings. However I can't seem to make the correct use of peek and get.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef SpellCheck_h
#define SpellCheck_h

#include <iostream>
#include <string>
#include <unordered_set>

bool isWhiteSpace(char blank);
void dePunctuate(std::string &dePunc);
bool isValidWord (std::unordered_set<std::string> dictionary, std::string valid);
bool finalPunctuation(char finPunc);





#endif /* SpellCheck_h */




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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  #include <iostream>
#include <unordered_set>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include "SpellCheck.h"

using namespace std;

bool isWhiteSpace(char blank){
    if (blank == ' '||blank == '-'||blank == '\t'||blank == '\n'){
        return true;
        
    }
    else {
        return false;
    }
}
void dePunctuate(std::string &dePunc){
    if (finalPunctuation (dePunc[dePunc.length()-1] == true)){
        dePunc.erase(dePunc.length()-1, 1);
    }
    if ( ('A' <= dePunc[0]) && (dePunc[0]) <= 'Z' )
        dePunc[0] += ('a' - 'A');
}
bool isValidWord (unordered_set<string> dictionary, string valid){
    if (dictionary.find(valid) != dictionary.end())
        return true;
    else
        return false;
}

bool finalPunctuation(char finPunc){
    if (finPunc == '.'||finPunc == '!'||finPunc == '?' ||finPunc == ';'||finPunc == '-'||finPunc == ','||finPunc == ':')
        return true;
    else
        return false;
    
}

int main(){
    vector<string> filecheck;
    ifstream dic;
    dic.open("dictionary.txt");
    
    
    unordered_set <string> dictionary;
    if(!dic.fail())
    {
        while (!dic.eof())
        {
            string a;
            dic>>a;
            // cout<<a<<" ";
        }
    }
    else
    {
        
        cout<<"this is fail";
    }
    string word;
    string nameOfFile;
    cout << "What is the name of file ";
    cin>>nameOfFile;
    fstream user;
    user.open(nameOfFile);
    if(user.fail())
    {
        cout<<"invalid filename";
    }
    else{
        char c =user.peek();
        string space;
        while (!user.eof()){
            
            while (isWhiteSpace(c) == true) {
                // user.get(c);
                //space += c;
                cout<<"it is true";
                
            }
            
            /* while (user.get()){
             if (isWhiteSpace(c))
             space += c;
             }*/
            
            filecheck.push_back(space);
            
            string copy_of_word = word;
            
            
        }
        for (auto i : filecheck){
            cout<<i;
        }
    }
}
Last edited on
istream::peek reads the next character in the input sequence, so you'd have to work at char level first before converting to std::string if your assignment so demands.
Below is a program to separate the input std::string into space and non-space std::vector<char> using peek() and get().
You can then convert the std::vectors (or at least the non-space std::vector<char>) to std::string but be aware that you'd have lost the words from the input string during the transition.
So to reconstitute the words from the std::vector<char> you'd have to keep a running tally of where in the input string the whitespaces occur and corresponding to those numbers you'd add up the chars to give back the words.
There are, of course, other (simpler) ways to parse a std::string by whitespace to split it into consituent words not involving peek(), get() etc:
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
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cctype>

int main()
{
    std::cout << "Enter line \n";
    std::string line{};
    getline(std::cin, line);
    std::istringstream stream(line);//http://www.cplusplus.com/reference/sstream/istringstream/
    std::vector<char> spaces{}, non_spaces{};

    while(stream)
    {
        if(std::isspace(stream.peek()))//http://www.cplusplus.com/reference/cctype/isspace/
        {
           char c = stream.get();
            spaces.push_back(c);
        }
        else
        {
            char c = stream.get();
            non_spaces.push_back(c);
        }
    }

   for (auto& elem : spaces)std::cout << elem ;
   std::cout << "\nNon-spaces: \n";
   for (auto& elem : non_spaces)std::cout << elem << " ";
}



Topic archived. No new replies allowed.