Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// I have the following code, but it doesn\'t work. #include <iostream> #include

ID: 3694500 • Letter: #

Question

// I have the following code, but it doesn't work.

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

using namespace std;

int main()
{
int i,j,len;
char title;
ifstream fp1("input.txt");
//FILE * fp1;
//fp1 = fopen("input.txt","r");
ofstream fp2("outpu1.txt");
ofstream fp3("output2.txt");

fp2<<"NAME: Sophyra "<<" "<<" DATE: 26-APR-2016"<<" "<<" Reversed Uppercase Strings";
fp3<<"NAME: Sophyra "<<" "<<" DATE: 26-APR-2016"<<" "<<" Pig Latin Strings";

char words[15][15];
char pigwords[15][15];
int numWords;
char str[80];

while(!feof(fp1))
{
getline(fp1,str);
int numwords=0;
char * pch;
pch = strtok (str," ");
while (pch != NULL)
{
strcpy(words[numwords],pch);
strcpy(pigwords[numwords],pch);
numwords++;
pch = strtok (NULL, " ");
}
  
for(i=0;i<numwords;i++)
{
len=strlen(words[i]);
char newWord[len];
char pignewWord[len];
int k=0,m=0;
for(j=0;j<len;j++)
{
if(!isalpha(words[i][j]))
{
newword[k++]=toUpper(words[i][j]);
pignewWord[m++]=toLower(words[i][j]);
}
}
newWord[k]='';
pignewWord[m]='';
strcpy(pigwords[i],pignewWord);
strrev(newWord);
strcpy(words[i],newWord);
}
char newLine[80];
char newLine1[80];
for(i=0;i<numwords;i++)
{
strcat(newLine,words[i]);
strcat(newLine," ");
strcat(newLine1,pigwords[i]);
strcat(newLine1," ");
}
fp2<<endl<<newLine<<endl;
fp3<<endl<<newline1<<endl;
  
}
fclose(fp1);

  
return 0;
}

Explanation / Answer

Answer:

Here are the errors in your code..check and rectify the code:

prog.cpp:26:8: error: no matching function for call to 'feof'
while(!feof(fp1))
^~~~
/usr/include/­stdio.h:828:12: note: candidate function not viable: no known conversion from 'ifstream' (aka 'basic_ifstream<char­>') to 'FILE *' (aka '_IO_FILE *') for 1st argument
extern int feof (FILE *__stream) __THROW __wur;
^
prog.cpp:28:1: error: no matching function for call to 'getline'
getline(fp1,str);
^~~~~~~
/usr/bin/../lib/gcc/­i586-linux-gnu/4.9/­../../../../include/­c++/4.9/bits/­basic_string.h:2812:5­: note: candidate template ignored: could not match 'basic_string<type-p­arameter-0-0, type-parameter-0-1, type-parameter-0-2>'­ against 'char [80]'
getline(basic_istrea­m<_CharT, _Traits>& __is,
^
/usr/bin/../lib/gcc/­i586-linux-gnu/4.9/­../../../../include/­c++/4.9/­streambuf:173:9: note: candidate function template not viable: requires 3 arguments, but 2 were provided
getline(basic_istrea­m<_CharT2, _Traits2>&,
^
/usr/include/­i386-linux-gnu/bits/­stdio.h:115:1: note: candidate function not viable: requires 3 arguments, but 2 were provided
getline (char **__lineptr, size_t *__n, FILE *__stream)
^
prog.cpp:50:1: error: use of undeclared identifier 'newword'; did you mean 'newWord'?
newword[k++]=toUpper­(words[i][j]);
^~~~~~~
newWord
prog.cpp:43:6: note: 'newWord' declared here
char newWord[len];
^
prog.cpp:50:14: error: use of undeclared identifier 'toUpper'; did you mean 'toupper'?
newword[k++]=toUpper­(words[i][j]);
^~~~~~~
toupper
/usr/include/­ctype.h:127:12: note: 'toupper' declared here
extern int toupper (int __c) __THROW;
^
prog.cpp:51:17: error: use of undeclared identifier 'toLower'; did you mean 'tolower'?
pignewWord[m++]=toLo­wer(words[i][j]);
^~~~~~~
tolower
/usr/include/­ctype.h:124:12: note: 'tolower' declared here
extern int tolower (int __c) __THROW;
^
prog.cpp:57:1: error: use of undeclared identifier 'strrev'; did you mean 'strlen'?
strrev(newWord);
^~~~~~
strlen
/usr/include/­string.h:399:15: note: 'strlen' declared here
extern size_t strlen (const char *__s)
^
prog.cpp:57:1: warning: ignoring return value of function declared with pure attribute [-Wunused-value]
strrev(newWord);
^~~~~~ ~~~~~~~
prog.cpp:70:12: error: use of undeclared identifier 'newline1'; did you mean 'newLine1'?
fp3<<endl<<newline1<­<endl;
^~~~~~~~
newLine1
prog.cpp:61:6: note: 'newLine1' declared here
char newLine1[80];
^
prog.cpp:73:1: error: no matching function for call to 'fclose'
fclose(fp1);
^~~~~~
/usr/include/­stdio.h:237:12: note: candidate function not viable: no known conversion from 'ifstream' (aka 'basic_ifstream<char­>') to 'FILE *' (aka '_IO_FILE *') for 1st argument
extern int fclose (FILE *__stream);
^