<< Given the file example name of "dafile".
Here the name is dafile
<< system("del dafile.txt");
And here the name is dafile.txt
Do you have a file named 'dafile' on your desktop and you are trying to delete 'dafile.txt', which does not exist?
<< Given the file example name of "dafile".
Here the name is dafile
<< system("del dafile.txt");
And here the name is dafile.txt
Do you have a file named 'dafile' on your desktop and you are trying to delete 'dafile.txt', which does not exist?
<< mitrmkar, in the code u gave me, what's the purpose of WordCount++?
WordCount counts the words in the file, the ++ operator increments the value
of WordCount by one, it could also be written as: WordCount = WordCount + 1.
<< And also, I need to reprint the file WITH the spaces (hence inFile.get(ch) ) AND with every 5th
<< word replaced with 10 underscores.
OK. I get it.
Then one solution is to read the file character by character like you already do. And using a std::string to accumulate the non-whitespace characters (i.e. other than '\n', ' ' and punctuation (there are actually more of those, but that's not important now))
So every time you hit a '\n', ' ' or punctuation, at that time, if you have any characters accumulated, that makes a word.
<snip>
string aword;
unsigned int count = 0;
while ( inFile.get(ch) )
{
// todo: check for a tabulator too
if ( ispunct(ch) || ( ch == ' ') || ( ch == '\n'))
{
if(aword.length())
{
// we have one or more characters, so that
// makes us a word ...
++ number;
++ count;
if( ! (count % 5))
{
// every fifth word is to be filtered out
cout << "_____";
// reset the counter
count = 0;
}
else
{
// output the current word
cout << aword;
}
// reset the word to empty
aword.erase();
}
// output the separating …
where is the code for printf and scanf fucntions...
One place you can start with is at the following address:
http://www.koders.com/c/fid4E0FA789459803C1E4369279B96E5C9B257941C8.aspx?s=printf#L7
That one is the GNU C's implementation. I think you probably find your way from there on to scanf. And doing a google on the file name gives more results I bet.
All the for loops use an upper limit of 100 but you only have 20 elements in intNum.RatNo[], that's why it fails.
A couple of things to fix ...
int main()
{
srand(time(NULL));
int sizeArray = rand () % MAX;
// at this point, sizeArray may very well be zero,
// probably you don't want that, so add a check against
// that condition ...
<snip>
}
int RationalInfo (int initialArray[][MAX], int size)
{ <snip>
for (int j = 0; j < size; j++)
{
// if initialArray[i+1][j] is zero, your program will fail here,
// you must not divide by zero, ever
initialArray[i+2][j] = initialArray[i][j] / initialArray[i+1][j];
// the same thing with % operator, initialArray[i+1][j] must not
// be zero here
initialArray[i+3][j] = initialArray[i][j] % initialArray[i+1][j];
}
return 1;
}
A minor note ... I think that actually
if(doc==EOF)break;
is OK since the compiler is to promote the char to int for the comparison.
float CelsiustoFahrenheit(int)
{
//
// This fails because you are dividing using two integers here,
//
// result = ((9/5)*temperatureC)+32;
//
// use the following instead
//
result = ((9/5[B].0[/B])*temperatureC)+32;
return result;
}
You might want to run the following snippet to see the difference
cout
<< "9/5 using integers is [ " << 9/5 << " ]" << endl
<< "9/5 using floating point is [ " << 9/5.0 << " ]" << endl;
The errors boil down to the RationalInfo() function, even though you are not calling
it from anywhere, the compiler wants it to be OK, so fix it first or remove it if don't need it
int RationalInfo (int initialArray[][B][MAX][/B])
{
[B]// You still need to declare the variables a and b here[/B]
int quotient, remainder;
float value;
for (int i = 0; i <setArray; i++)
{
for (int j = 0; j < MAX; j++)
{
initialArray [i+2][j] = initialArray [i][j] / initialArray [i+1][j];
quotient = a/b [B];[/B]
remainder = a%b [B];[/B]
value = a/b [B];[/B]
}
}
[B]// You need to return something from this function[/B]
return ???
}
Maybe you get some ideas/clues from below, the important changes are that
the file is read in one word at a time + the use of operator %
>> i don't think std::string word is something we covered.
Hmm, you already have there a std::string, namely the 'cloze'.
...
string aWord;
unsigned int WordCount = 0;
// Read the file in word by word
// (a 'word' is here anything non-whitespace)
while (inFile >> aWord) // While inFile succeeds in reading a word from the file
{
// output the word
cout << aWord;
WordCount ++;
}
inFile.clear(); // Move read pointer to beginning
inFile.seekg(0L, ios::beg);
unsigned int Count = 0;
// re-read it again the same way as above
while (inFile >> aWord) // While inFile succeeds in reading a word from the file
{
++ Count;
// every 5th word is to be replaced,
// use the modulus operator (%)
if( ! (Count % 5))
{
// replace it here
// reset counter ...
Count = 0;
}
else
{
// don't replace
}
}
return 0 ;
}
>> may I have a clarification on what's going on wrong?
You had misplaced the match() function inside the main() function, that is prohibited.
You also had taken out the return statements from the match() function, now they are restored.
So try the following ...
bool match(const std::string& search_word, const std::string& word)
{
if(search_word.size() <= word.size())
{
std::size_t N = word.size() - search_word.size();
for(std::size_t pos=0; pos <= N; ++pos)
{
std::size_t i = 0;
for( ; i<search_word.size(); ++i)
{
if( (search_word[i] != '!' ) && ( search_word[i] != word[pos+i] )) break;
}
if(i == search_word.size())
return true;
}
}
return false;
}
int main () {
string search_word = "!h!";
string word = "within";
// Do the search by calling the function ...
bool bMatch = match(search_word, word);
// The bool value is now either true or false, so here you can test
// whether there was a match and output the result
if(true == bMatch)
{
cout << "A match .." << endl;
}
else
{
cout << "no match .." << endl;
}
return 0;
}
Hi,
How about just forgetting the Get/SetWindowxxx functions and use something like ...
SendMessage(edit_control, WM_CHAR, achar, 0);
Please take a closer look at the file, you have found the first usage step i.e.
Usage: 1. Rename this file to ...
but there is more to it, i.e. the step 2 says:
2. Compile and/or run with appropriate compiler and operating system
Under Windows, the instruction to compile and run it (as an executable application) is a must. It really makes no sense at all just to rename the file to something that looks like an executable application (i.e. a .COM).
Thank you so much everyone for the help. There is still one persisting problem though...if you let c3 be a high color like A or W which are worth 8 and 9 respectively, the result is -2147483648. It works for everything else, just not when the last color is a high number.
Change the code so that it works with the 'double' type instead of 'int'. A double should be able to represent the maximum value that the program produces.
I.e. change the stuff that is in bold below ...
[B]double[/B] calcRval(int,int,int);
[B]double[/B] colCode(char);
[B]double[/B] calcRval(int c1,int c2, int c3)
{
[B]double[/B] v1,v2,v3,v;
...
-------------------------------------------------
[B]double[/B] colCode(char colCode)
{
...
-------------------------------------------------
and in the main() function ...
[B]double[/B] val = calcRval(c1, c2, c3);
// set the precision for output
[B]cout.precision(14);[/B]
cout << "Value = " << val << endl;
Everything seems to be going okay... except for the last part when I'm trying to get the word_appearances to count how many of the search words are in the poem written inside script.txt.
You need only two for() loops to iterate over the strings i.e. something like below.
int word_appearances = 0;
// i must be less than 10 and some string is needed at the index (i)
for(i=0; i < 10 && words[i].length(); i++)
{
// j must be less than 16 and some string is needed at the index (j)
for(j=0; j < 16 && poem[j].length(); j++)
{
// are the words are identical
if(poem[j] == words[i])
{
// yes they are
cout << "match: " << words[i] << endl;
word_appearances ++;
}
}
}
cout << word_appearances << endl;
I don't understand how this program acts as a COM application, that's the problem.
Well, you tried to run it as a .COM but you did not compile it first.
Any file with the extension .com is a candidate to be run as a program under windows.
Windows will try to run it, but obviously fail, because it is just a text file instead of a compiled program.
So, to try it out properly, rename the file to e.g. polyglot.c, and try to produce an executable program using a C compiler/linker.
If the printer behaviour really happened because of you 'running' polyglot.com, it is quite amusing actually - LOL.
Hi,
I quickly changed the code so that it should do what you intented.
#include <iostream>
#include <cmath>
using namespace std;
int calcRval(int,int,int);
// the input to colCode() is now a char
int colCode(char);
// these variables are not needed
// int c1,c2,c3,val;
int main()
{
char c1,c2,c3;
cout<<"input c1:";cin>>c1;
cout<<"input c2:";cin>>c2;
cout<<"input c3:";cin>>c3;
int val=calcRval(c1,c2,c3);
// the signature "int main()" implies that we
// should return an int, so let's do so
return val;
}
int calcRval(int c1,int c2, int c3)
{
int v1,v2,v3,v;
v1=colCode(c1);
v2=colCode(c2);
v3=colCode(c3);
// note, that there is no validation against invalid input,
// i.e. v1 .. v3 may very well be -1 here ...
v=((10*v1+v2)*pow(10,v3));
return v;
} // Here ends the calcRval function
int colCode(char colCode)
{
// map the color code letter to a value
switch(colCode)
{
case 'B': return 0;
case 'N': return 1;
case 'R': return 2;
case 'O': return 3;
case 'Y': return 4;
case 'G': return 5;
case 'E': return 6;
case 'V' :return 7;
case 'A': return 8;
case 'W': return 9;
default:
// invalid color
break;
} // Here ends the switch
cerr << "Ooops, invalid color code: " << colCode << endl;
// This means invalid color
return -1;
} // Here ends the colCode function