trying to write a code to change vowels to numbers

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
ifstream member;
ofstream dogs;
ofstream cats;
int n=0;
int r=0,f=1,g=2,k=3,p=4,h=5;
string x[100];
int y[100];
dogs.open("file.txt",ios::out);
member.open ("file.txt",ios::in);
cats.open("file2.txt",ios::out);
member>> x[n];
while (x[n]=="a"||x[n]=="e"||x[n]=="i"||x[n]=="o"||x[n]=="u")
{
if(x[n]=="a")
{y[n]=f;}
if(x[n]=="e")
{y[n]=g;}
if(x[n]=="i")
{y[n]=k;}
if(x[n]=="o")
{y[n]=p;}
if(x[n]=="u")
{y[n]=h;}
cout<<y[n] <<"";
n++;
}
member.close();
for(int s=0;s<r;s++)
{cats<<y[s]<<"";}
}
I think you have a problem?

The problem is that member>> x[n]; is outside the loop?
If so consider a do { } while(...); loop


Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
I had it inside the loop but the program does nothing
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
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
    ifstream member;
    ofstream dogs;
    ofstream cats;
    int n=0;
    int r=0,f=1,g=2,k=3,p=4,h=5;
    string x[100];
    int y[100];
    dogs.open("file.txt",ios::out);
    member.open ("file.txt",ios::in);
    cats.open("file2.txt",ios::out);
    
    while (x[n]=="a"||x[n]=="e"||x[n]=="i"||x[n]=="o"||x[n]=="u")
    {member>> x[n];
    if(x[n]=="a")
    {y[n]=f;}
    if(x[n]=="e")
    {y[n]=g;}
    if(x[n]=="i")
    {y[n]=k;}
    if(x[n]=="o")
    {y[n]=p;}
    if(x[n]=="u")
    {y[n]=h;}
    cout<<y[n] <<"";
    n++;
    }
    member.close();
    for(int s=0;s<r;s++)
    {cats<<y[s]<<"";}

    return 0;
}

sorry about the plain text, here's the code.
I had it inside the loop but the program does nothing
certainly. You check the variable before it is set. therefore my do...while suggestion. I don't see why you need the comparison on line 18. Try this:
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
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
    ifstream member;
    ofstream dogs;
    ofstream cats;
    int n=0;
    int r=0,f=1,g=2,k=3,p=4,h=5;
    string x[100];
    int y[100];
    dogs.open("file.txt",ios::out);
    member.open ("file.txt",ios::in);
    cats.open("file2.txt",ios::out);
    
    while (member>> x[n])
    {
    if(x[n]=="a")
    {y[n]=f;}
    if(x[n]=="e")
    {y[n]=g;}
    if(x[n]=="i")
    {y[n]=k;}
    if(x[n]=="o")
    {y[n]=p;}
    if(x[n]=="u")
    {y[n]=h;}
    cout<<y[n] <<"";
    n++;
    }
    member.close();
    for(int s=0;s<r;s++)
    {cats<<y[s]<<"";}

    return 0;
}
x need not to be an array
1)If you don't want to use x any further hen don't use it as array
2)why use f,g,k,p,h???You know 1,2,3 etc and can directly use it.
3)@coder777 I think he need to end file reading if non-vovel is encounered.
But if so then also jaytee use do...while or simply initialise xas any vovel.(considering that you are not using it as array and wasting memory.
Topic archived. No new replies allowed.