Help with file input output

currently only outputs the very last input string ex- Hello darkness, my old friend. good to see you again. output file- againway. What am missing? am i using fin.getline wrong? i figure its either that or, I'm way off. thanks any help or feedback is appreciated.

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


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <fstream>

using namespace std;


bool IsVowel(char letter)
{
	switch (letter)
	{
	case 'A':
	case 'E':
	case 'I':
	case 'O':
	case 'U':
	case 'Y':
	case 'a':
	case 'e':
	case 'i':
	case 'o':
	case 'u':
	case 'y':
		return true;

	default:
		return false;
	}
}

void PigLatin(char *word)
{
  ifstream fin;
  fin.open("pigLatinFilein.txt");
  ofstream fout;
  fout.open("pigLatinFileout.txt");

    string s1(word);
	string s2;

	if (IsVowel(word[0]) == true) s2 = s1 + "way";
	else s2 = s1.substr(1) + s1[0] + "ay";



	fout << s2 << " ";
}
int main()
{

  ifstream fin;
  fin.open("pigLatinFilein.txt");
  ofstream fout;
  fout.open("pigLatinFileout.txt");

  	char sentence[10000];
	char *words;


	fin.getline(sentence, 10000);
	words = strtok(sentence, " ,.!:;""?");

	while (words != NULL)
	{
		PigLatin(words);
		words = strtok(NULL, " ,.!:;""?");
	}
    fin.close();
	return 0;

}

//1. If a word starts with a consonant and a vowel, put the first letter of the word at the end of the word and add "ay."

//Example: Happy = appyh + ay = appyhay

//2. If a word starts with a vowel add the word "way" at the end of the word.

//Example: Awesome = Awesome +way = Awesomeway 

Your main problem is opening the files again in PigLatin. It would be best if it just returned the modified string. Also, you aren't including the double-quote in your delimiters properly. To include a double-quote inside double-quotes you need to precede it with a backslash.

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

bool IsVowel(char letter)
{
    switch (letter)
    {
    case 'A': case 'E': case 'I': case 'O': case 'U': //case 'Y':
    case 'a': case 'e': case 'i': case 'o': case 'u': //case 'y':
        return true;
    }
    return false;
}

string PigLatin(const char *word)
{
    string s(word);
    if (IsVowel(word[0]))
        s = s + "way";
    else
        s = s.substr(1) + s[0] + "ay";
    return s;
}

int main()
{
    const char *delims = " ,.!:;\"?";

    ifstream fin("pigLatinFilein.txt");
    ofstream fout("pigLatinFileout.txt");

    char sentence[10000];
    fin.getline(sentence, sizeof sentence);

    char *word = strtok(sentence, delims);
    while (word)
    {
        fout << PigLatin(word) << ' ';
        word = strtok(NULL, delims);
    }
    fout << '\n';
}

ah, thank you so much
Topic archived. No new replies allowed.