Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I use cout. I get all the information. When I use ofstream out("") it only prints the last line.

What I have tried:

#include<iostream> //i.o
#include<cctype> //for isdigit
#include<sstream> //to phrase string
#include<string>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
#include<bits/stdc++.h>
#include<iostream> //i.o
#include<cctype> //for isdigit
#include<sstream> //to phrase string
#include<string>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;

void splitString(string str)
{ofstream out("example.txt");
    string alpha, num, special;
    for (int i=0; i<str.length(); i++)
    {

         if((str[i] >= 'A' && str[i] <= 'Z') ||
                (str[i] >= 'a' && str[i] <= 'z'))
           break;

           else if (isdigit(str[i]))
            num.push_back(str[i]);
    }


    out << num<<endl;

}

// Driver code
int main()
{

std::string word;
    std::vector<std::string> file;

    std::ifstream in( "Files.txt" );

    while ( in >> word )
        file.push_back( word );

    for ( size_t i=0; i<file.size(); i++ ){




    string str = file.at(i);
    splitString(str);}
    return 0;
}
Posted
Updated 12-May-18 22:15pm

It is because you are creating a new ofstream object every time you call splitString. So it opens the file and writes just the data that is extracted in the function. You should create the stream n your main function, and pass the reference to splitString as a second parameter. Alternatively, do all the file writing in main, and make splitString return the string that it creates.
 
Share this answer
 
C++
string alpha, num, special;
...
num.push_back(str[i]);

Last time I checked, .push_back is related to vectors, not stings. Probably the reason to not get the expected result.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Richard MacCutchan 13-May-18 7:47am    
No, push_back works for strings also. And if you read my answer you can see the solution is fairly simple, but the debugger would not have found it.
Patrice T 13-May-18 9:42am    
ok, my bad.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900