| | |
Delete words error!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2004
Posts: 65
Reputation:
Solved Threads: 0
I've just wrote this program and when i compile, there is an error at the red color fonts.
Can i know what's the error?
thank you.
char quest;
char words[50];
int count;
int z;
ofstream file;
file.open("WORDS.txt" , ios::app);
file.close();
cin.ignore(50,'\n');
cout << "\n\n Please enter the word that need to be deleted: ";
cin.getline(words,50,'\n');
for (int i=0; i<strlen(words); i++)
{
words[i]= tolower(words[i]);
z = count + 5;
}
for (i=0; i < count; i++)
{
if ( strcmp( words[i],words) == 0 )
z = i;
}
if (z<count)
{
ofstream file;
file.open("WORDS.txt", ios::app);
for ( i=0; i < count; i++)
{
if ( i!=z )
cout << words[i] << endl;
}
system("del words.txt");
system("ren words.txt words.txt");
}
Can i know what's the error?
thank you.
char quest;
char words[50];
int count;
int z;
ofstream file;
file.open("WORDS.txt" , ios::app);
file.close();
cin.ignore(50,'\n');
cout << "\n\n Please enter the word that need to be deleted: ";
cin.getline(words,50,'\n');
for (int i=0; i<strlen(words); i++)
{
words[i]= tolower(words[i]);
z = count + 5;
}
for (i=0; i < count; i++)
{
if ( strcmp( words[i],words) == 0 )
z = i;
}
if (z<count)
{
ofstream file;
file.open("WORDS.txt", ios::app);
for ( i=0; i < count; i++)
{
if ( i!=z )
cout << words[i] << endl;
}
system("del words.txt");
system("ren words.txt words.txt");
}
ah it's simple, just do for(int i=0; i<.......
A for loop has it's own scope so you have to declare the i again.
A for loop has it's own scope so you have to declare the i again.
•
•
Join Date: May 2004
Posts: 95
Reputation:
Solved Threads: 10
The second parameter to strcmp up there is actually a pointer--that's half of what an array is, just a location in memory (the other half is the field size, in this case char, to use when making offsets into the array).
A few additional comments:
The value of count doesn't change, so it doesn't have to be in the loop--if that line assigning to z is really what you want to do.
Similar idea here. The loop will always end with i equal to count - 1, so making that last assignment to z doesn't have to be inside the loop--again, if that's what you want it to do.
Continuing the same line of thought, z<count will always be true, so the if statement isn't necessary.
This last bit is a little strange--renaming a file that no longer exists?
--sg
•
•
•
•
Originally Posted by koh
...
if ( strcmp( words[i],words) == 0 )
...
error message says:
error c2664: 'strcmp' : cannot convert parameter 1 from 'char' to 'const char*'
..but then, i didn't use pointers
The value of count doesn't change, so it doesn't have to be in the loop--if that line assigning to z is really what you want to do.
•
•
•
•
Originally Posted by koh
...
for (int i=0; i<strlen(words); i++)
{
words[i]= tolower(words[i]);
z = count + 5;
}
...
•
•
•
•
Originally Posted by koh
...
for (i=0; i < count; i++)
{
if ( strcmp( words[i],words) == 0 )
z = i;
}
...
•
•
•
•
Originally Posted by koh
...
if (z<count)
{
ofstream file;
file.open("WORDS.txt", ios::app);
...
This last bit is a little strange--renaming a file that no longer exists?
•
•
•
•
Originally Posted by koh
...
system("del words.txt");
system("ren words.txt words.txt");
}
...
![]() |
Similar Threads
- how to delete windows live search words? (Windows NT / 2000 / XP)
- SQL Delete/Update Error (ColdFusion)
- I cannot delete a folder (error message) (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Faulty programme!!!
- Next Thread: Is C++ More Difficult Than Calculus?
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





