Stop printing when meet Space or endl?

Hello :)
Am trying to make a program which opens a txt file, searches a word and displays a single word behind it.
Got me the folowing txt file as test
bla bla bla
bleh bla bo
name Jos
bla bla bla
bohoboabb


This is the code I got so far.
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
void SearchName()//Searches for 'name' 
{
	int x =0;
	ifstream myFile("test.txt"); //open test.txt
	
	for (int i = 0;i < 50; i++) //Reads text file
	{
		char ch;
		myFile >> ch; // assign ch to a char in test.txt
		
		if (ch == 'n'&& x == 0)
			{ x=1; continue;}
		
		if (ch == 'a'&& x == 1)
			{ x=2; continue;}
		else if (x==1)  {x = 0;}

		if (ch == 'm'&& x == 2)
			{ x=3; continue;}
		else if (x==2)  {x = 0;}

		if (ch == 'e'&& x == 3)
			{ x=4; continue;}
		else if (x==3)  {x = 0;}

		if (x == 4 )
			{ cout << ch ;}
	}
}

Output:
Josbla bla blabohoboabbbbbbbbb

But got no idea how to even start making it print the next word and stop when it meets a 'space' or a 'New Line (endl)'

So my question would be: How can I make it stop printing when it meets a space or a endl ?

(so far only tried with using ascii commands, but appearentely it won't work like that:/)

Thanks on advance :)
closed account (zb0S216C)
x never changes it's value, so it's always 0. What exactly are you trying to achieve here?
Doing this one char at a time is insane.
Use strings
1
2
3
4
5
6
7
string str;
while(file >> str){
   if(str == "name"){
      if(file >> str) cout << str;
      else cout << "that was the last word\n"
   }
}

You could do this with c-strings, but this is a lot cleaner.
@Framework: x becomes 4 when if finds 'name' in the txt file :)

@hamsterman : Somewhere I knew I was doing it the hard way again :p
Anyways ,you'd do it like this then ? 'cause in my case it gives errors :/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void SearchName()//Searches for 'name' 
{
	ifstream file("test.txt"); //open test.txt
	
	string str;
	while(file >> str)
	{
		if(str == "name")
		{
			if(file >> str)
			{ 
				cout << str;
			}
			else cout << "that was the last word\n";
   
		}
	}
}


And thanks for the replies :)

Edit: sorry forgot to include the header ... anyways, it doesn't display the word after 'name' atm
Last edited on
closed account (zb0S216C)
My mind is still foggy on what you're trying to achieve. What do you do with the data extracted from the file stream?
My appologies for not answering your second question.

Let's assume we have a list like this :
this is a text file that contains information about people
person 1 : name Jos
age 30
gender male
...
person 2 : name Frank
age 70
gender male
...

And I would want to have my program print all the names inside the the file like this:
output:
Jos
Frank


Let's say I want to make this :D

The problem : Can't figure out how to print a single word taken out of a large text(in our case : Jos) , Don't know how to make it stop printing at a 'space' or at the end of the line.

Hope this clarifies my problem :)
closed account (zb0S216C)
Sounds like a parsing job to me.
Uhm it sort of is :p
But I'm trying to make this to gain more experiance with .txt editing.
So if you know how I can make this, please tell me then :)
closed account (zb0S216C)
See if this works for you:
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
#include <fstream>
#include <stdio.h>
#include <vector>
#include <string>

using std::ifstream;
using std::string;
using std::vector;

struct Person
{
    Person( const string &ThisName, const int &ThisAge, const string &ThisGender )
    {
        Name = ThisName;
        Age = ThisAge;
        Gender = ThisGender;
    }

    string Name;
    int Age;
    string Gender;
};

vector < Person > People;

void LoadFile( const string &File )
{
    ifstream Source;

    // Open the file for reading.
    Source.open( File.c_str( ) );

    // Is the file open?
    if( Source.is_open( ) == false )
    {
        // Failed.
        printf( "Failed to open the given file.\n" );
        return;
    }

    // Command buffer.
    string Command;

    // File field buffers.
    string NewName( "NULL" ), NewGender( "NULL" );
    int NewAge( 0 );

    // Extract data for 1 person.
    for( unsigned Pass( 0 ); Pass < 1; Pass++ )
    {
        for( unsigned Field( 0 ); Field < 3; Field++ )
        {
            Source >> Command;
            if( Command == "Person" )
            {
                // Extract the name.
                Source >> NewName;
            }

            else if( Command == "Age" )
            {
                // Extract the age.
                Source >> NewAge;
            }

            else if( Command == "Gender" )
            {
                // Extract the gender.
                Source >> NewGender;
            }

            else
            {
                // Assume it's a comment.
            }
        }

        Person NewPerson( NewName, NewAge, NewGender );

        // Add the person.
        People.push_back( NewPerson );
    }

    Source.close( );
}

int main( )
{
    LoadFile( "File.txt" );
    for( unsigned Index( 0 ); Index < People.size( ); Index++ )
    {
        printf( "Name:\t\t%s.\n", People.at( Index ).Name.c_str( ) );
        printf( "Age:\t\t%i.\n", People.at( Index ).Age );
        printf( "Gender:\t\t%s.\n", People.at( Index ).Gender.c_str( ) );
    }

    return 0;
}


Edit-------------------------8<------------------------

This is the file I was reading from.
Person Jos
Age 30
Gender male


Last edited on
Hmm, I suppose this does answer my example, but with my current programming experiance, this looks like a completely different language :o.

Is it possable to make this with these headers ?

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

Kind of learned c++ with these :)

And thanks for the replies
closed account (zb0S216C)
It is possible. If you don't use the vector then you will have to improvise with a dynamic array or just miss-out the array completely.
Faff, my code wasn't tested, but I don't see why it wouldn't work.. Could you tell me what it did print?

Framework, avoid mixing iostream with stdio. It's just weird to use two libraries for one thing.. Also, the correct name for stdio in c++ is <cstdio>.
@hamsterman: it worked :) , sry seems like the txt file contained nameJos ...
Really Thanks! :D

But now, the original problem :)
How can I make it print nothing (or error msg) if the after the 'name' the line ends?

assume this txt file:
name Jos
name
name bla bla
name


Prog should output this :)
Jos
bla


Current prog outputs this now :/
Josnamethat was the last word


How could I do that ?
Last edited on
Topic archived. No new replies allowed.