C++ program works fine locally but gives me errors when I submit it as homework due to -Wfatalerrors
Apr 16, 2015 at 5:37pm UTC
Title. The program uses parallel arrays and sorts children and parents into sides. The error I'm getting is:
parents.cpp: In function 'int main()':
parents.cpp:37: error: 'class MyStream' has no member named 'ignore'
compilation terminated due to -Wfatal-errors.
and my code is:
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 101 102 103 104 105 106 107
#include <iostream>
#include <string>
using namespace std;
int len(string s) {
int i=0;
while (s[i]!=0)
++i;
return i;
}
bool less_str(string s1, string s2) {
int i=0;
while (s1[i]!=0 && s2[i]!=0) {
if (toupper(s1[i]) < toupper(s2[i]))
return true ;
if (toupper(s1[i]) > toupper(s2[i]))
return false ;
++i;
}
if (s1[i]==0)
return true ;
return false ;
}
int main() {
string children[512], parents[512];
cout << "This program prints parent-child pairs.\nEnter parents and children below, use 'quit' to stop.\n" ;
int n=0;
while (1) {
string tmp, tmpc, tmpp;
cout << "Parent: " ;
cin >> tmpp;
if (tmpp == "quit" )
break ;
cout << "Children of " << tmpp <<": " ;
cin.ignore();
getline(cin,tmpc);
int k = 0;
while (tmpc[k]!=0) {
tmp="" ;
while (tmpc[k]!=0 && tmpc[k]!=' ' ) {
tmp += tmpc[k];
++k;
}
if (tmpc[k]==' ' )
++k;
parents[n] = tmpp;
children[n] = tmp;
++n;
}
}
for (int i=0; i<n; ++i) {
bool in=false ;
for (int j=0; j<n; ++j)
if (children[j]==parents[i]) {
in=true ;
break ;
}
if (!in) {
children[n]=parents[i];
parents[n]="" ;
++n;
}
}
for (int i=1; i<n; ++i) {
int j = i;
while (j>0 && less_str(children[j], children[j-1])) {
string t1=children[j], t2=parents[j];
children[j]=children[j-1];
parents[j]=parents[j-1];
children[j-1]=t1;
parents[j-1]=t2;
--j;
}
}
int longest_child=5;
for (int i=0; i<n; ++i)
if (len(children[i]) > longest_child)
longest_child = len(children[i]);
++longest_child;
cout << endl;
string out ="" ;
out += "Child" ;
for (int i=5; i<longest_child; ++i)
out+=" " ;
out += "Parent\n-----" ;
for (int i=5; i<longest_child; ++i)
out+=" " ;
out+="------\n" ;
cout<<out;
for (int i=0; i<n; ++i) {
out="" ;
out += children[i];
for (int j=len(children[i]); j<longest_child; ++j)
out+=" " ;
out += parents[i] + "\n" ;
cout<<out;
}
return 0;
}
Apr 16, 2015 at 5:51pm UTC
Change line 37 to std::cin.ignore();
. Not exactly sure what MyStream is
Apr 16, 2015 at 5:51pm UTC
The code has no compilation errors. Are you sure you are submitting the correct file? There is no mention of "MyStream " in your code. What happens if you change line 37 to std::cin.ignore();
?
Apr 16, 2015 at 7:12pm UTC
Now I get
parents.cpp: In function 'int main()':
parents.cpp:37: error: '__my_cin' is not a member of 'std'
compilation terminated due to -Wfatal-errors.
with std::cin.ignore();
Apr 16, 2015 at 7:28pm UTC
Do you know what compiler is being used to verify your assignment? It sounds like someone took GCC and mutilated it.
Apr 16, 2015 at 7:37pm UTC
Unfortunately, I have no idea what they're using. Locally I'm using the default compiler in Dev-C++.
Apr 16, 2015 at 8:49pm UTC
Have you tried talking to your professor or emailing them to find out what's going on? Just copy the errors you are getting and they will probably have a copypaste answer for you.
Topic archived. No new replies allowed.