How to square and cube a number in my program?

#include <iostream>
#include <conio.h>
#include <iostream>
using namespace std;

int main()
{
FILE *pFile, *upperFile, *lowerFile, *symbolFile, *squareFile, *cubeFile;

int c;
int space=0;

pFile=fopen ("my_input.txt","r");
upperFile=fopen ("uppercase.txt","w");
lowerFile=fopen ("lowercase.txt","w");
symbolFile=fopen ("symbol.txt","w");
squareFile=fopen ("square.txt","w");
cubeFile=fopen ("cube.txt","w");

if (pFile==NULL) perror ("Error opening file");

else
{
do {
c = fgetc (pFile);
if (isupper(c))
{
c = tolower(c);
fputc((int) c,upperFile);
}
else if (islower(c))
{
c = toupper(c);

fputc((int) c,lowerFile);
}

else if (isdigit(c))
{
if(c%2==0)
{
fputc((int) c, squareFile);
{

}


}
else
{
fputc((int) c, cubeFile);
}
}
else
{
fputc((int) c, symbolFile);

}


} while (c != EOF);
fclose (pFile);

}
getch();
return 0;
}




I can't find a way to square and cube the number I want, which the number will show in the notepad. Is there a way to cube and square a number in my program??
Last edited on
one way to do something like 73 is using the pow function in the cmath header. try something like this:

1
2
3
4
5
6
7
8
9
#include <cmath> //includes several different functions to do with maths
#include <iostream>

int main()
{
std::cout << pow(7, 3) << std::endl;

return 0;
}


pow works like this: pow(/*base number*/, /*power*/)



PS please use code tags
Last edited on
Thanks for the info mr. Cronnoc but it doesnt work on my program, anyways this is the problem

"Write a program that reads in a text file named “my_input.txt” that contains characters composed of digits, letters and other symbols. Then it processes the contents of the file by examining each of the characters of the file. If the character read is an alphabet, it checks its case. If it is in lowercase, then its uppercase is sent to a file named “uppercase.txt”; if in uppercase, its lowercase is sent to “lowercase.txt.” But if the character is a symbol, it is sent to “symbols.txt.” Lastly, if it is an integer, it sends its square to a file named “square.txt”, if it is even. Otherwise, it sends its cube to a file named “cube.txt” if it is odd.
"
Last edited on
oh well i tried
Last edited on
Its alright :)) I'm just having a hard time in this problem

"Lastly, if it is an integer, it sends its square to a file named “square.txt”, if it is even. Otherwise, it sends its cube to a file named “cube.txt” if it is odd."
Thanks anyways :D
Topic archived. No new replies allowed.