Yes, that can be a possibility.
Try doing Run -> dxdiag and run all the DirectX related tests. They will let you know whether your card supports DX10 or not.
Yes, that can be a possibility.
Try doing Run -> dxdiag and run all the DirectX related tests. They will let you know whether your card supports DX10 or not.
Like I said before, you need to read in the entire line in a string and start parsing it yourself, treating the string as an array of characters. For this, you will need functions which help you in determining whether a character is a space character or not. The function I am talking about is isspace().
for(int i = 0; i < myString.size(); ++i)
{
//means that the character is nonwhitespace, so the string must contain other characters
if(isspace(myString[i]) == 0)
{
stringHasCharacters = true;
break;
}
}
Oh and btw, just call him Joey, he really likes it. :D
So I guess the only option left is to manually parse the string character by character and draw your own results. Best of luck.
Have you been taught the find function in C++? If so then you can search the string for the character which doesn't belong to the family of whitespace characters. If you find one, write the line as it is, and if no character other than whitespace character is found (indicates blank line) just indent and move on to the next line.
//check for a blank line
if(find_first_not_of(" \t\r\f\n\v") == string::npos)
{
//write a indent to the new file
}
else
{
// write the line as it is
}
I don't know what you are trying to achieve by doing if(!'\n').
This condition will never be true hence the whole file is given out as it is.
Also the trouble with this approach is that even lines which are not preceded by newlines but have indentations will always look indented. So you would have no way of knowing which lines were indented by the algorithm or which lines were indented from the start.
For proper formatting, trimming is a must.
Replace the round brackets with square ones [] and you are good to go...Oh and btw it would be a good idea to initialize your arrays.
getchar is a standard C /C++ functiona used for getting data from the standard input stream (in normal cases keyboard). It is buffered in the sense it does not see the characters unless the user presses the return key. Since we want to prevent the console window from disappearing after the program execution, we put it at the end of the program before the return statement if any is present.
His name is 'good old Ancient Dragon'... :D
Like I said before, trimming the string before processsing it is the way to go in your case. After trimming the string, if its length is zero (check only for length and not the other two), then flip the binary flag.
Maybe something like this: (not tested)
void leftTrim(string& inStr, const string delimiters = " \t\n\r\v\f")
{
string::size_type pos = inStr.find_first_not_of(delimiters);
inStr.erase(0, pos);
}
int main ()
{
string line;
bool flag = true;
ifstream inFile("test.txt");
ofstream outFile("output.txt");
while(getline(inFile, line))
{
leftTrim(line);
if(line.size() == 0)
{
flag = true;
continue; //skip current line and move to next one
}
if(flag) //if a new paragraph is afoot
{
outFile << endl << " " << line << endl;
flag = false;
}
else //its a normal line
{
outFile << line << endl;
}
}
//close the streams and wait for keypress
inFile.close();
outFile.close();
getchar ();
}
Don't forget to consider the following situations:
• The blank lines may be composed of only spaces. You might need to write a trim function which first trims the lines read and then processes them.
• The first line can be a non blank requiring you to initialize the flag accordingly.
The trim function can be as simple as (not my function):
#include <string>
const std::string whiteSpaces( " \f\n\r\t\v" );
void trimRight( std::string& str,
const std::string& trimChars = whiteSpaces )
{
std::string::size_type pos = str.find_last_not_of( trimChars );
str.erase( pos + 1 );
}
void trimLeft( std::string& str,
const std::string& trimChars = whiteSpaces )
{
std::string::size_type pos = str.find_first_not_of( trimChars );
str.erase( 0, pos );
}
void trim( std::string& str, const std::string& trimChars = whiteSpaces )
{
trimRight( str, trimChars );
trimLeft( str, trimChars );
}
system
function calls in C / C++ invoke the operating system functionality (here cls
) so the context has to be changed and control be handed from the currently running program to the Operating System to invoke the clear screen function.
Using the method posted by Joey won't invoke OS functions and hence even though it looks a bit length, its performance is way better than the system call.
Read this.
I wonder what BS here stands for... ;)
How would you account for those millions of lines of code which use the old C functions, wrapped up in the new standard namespace? And like I pointed out getchar IS C++ otherwise it wouldn't have been included in the C++ standard namespace. Call it backward compatibility if you like, but thats how the situation stands.
The good thing about C / C++ is its flexibility and its the flexibility which kills the beginners. You can as well mix fgets
and cin >>
and still get away with it, no problems as such.
Oh and btw, getchar is C++ . Just include cstdio and you are good to go. The fact that getchar is included in the standard namespace makes it a C++ standard function. Call it backward compatibility or consideration for C programmers if you want, but IMHO getchar
is C++. If you will notice C++ has no direct character manipulation and processing functions. The header cstdlib
, ctype
and cstring
seem to do that job pretty well... ;)
> In C. It's not a good idea to use that in C++, use cin.get() instead.
Any good reason to back that up ? ;)
Oh, I believe processing cycles should be saved whenever possible.
Consider the input: 4384023824039483043840aadf348304923840394sdffdfd8304382409243464565204
Might seem a bit nonsense, but still happens. Or worse, if this function is applied to each and every line of the data file which is full of numerals. It helps me in saving or not performing the redundant computations (that would be 5 comparisions in worst case) whenever possible at little or no extra cost.
But then again, a matter of perspective and style.
Something like this should make your professor happy:
for (int i = 0; i < length; ++i)
{
ch = tolower (line [i]);
if (isalpha (ch) &&
(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'))
{
++vowelCount;
}
}
Why restrict the user by asking him to enter a "period" after the sentence ? Replace your while loop with a for loop, something like this:
int length = str.length ();
for (int i = 0; i < length; ++i)
{
// do processing
}
Accept the whole sentence from the user in the form of a string and pass the string to this function, which will loop through each character and find the vowel count.
Just replace the ++current
in my previous code with current += 2
and make the data type of the variable value
as double
and you should be good to go.
Maybe something like this:
double factorial (double value)
{
double result = 1.0;
while (fabs (value) > 0)
{
result *= value--;
}
return result;
}
int main ()
{
double summand = 0.0, sum = 0.0;
int value = 0, current = 0;
bool flag = true;
cout << "Enter the power of e whose answer you want: " ;
cin >> value;
getchar ();
do
{
summand = pow ((double)value, (double)current) / factorial (current);
if (flag)
sum += summand;
else
sum -= summand;
flag = !flag;
++current;
}
while (summand > 0.00001);
cout << "\nAnswer is : " << sum;
getchar ();
return 0;
}
I still see a while loop in your code. On top of that the while loop uses the value of i which is 5 when the while block is entered (since you never reset it after the for loop).
Stop checking for the occurance of zero in the loop. Just make it like the loop you used for accepting user input.
The alteration is happening alright. Your logic is wrong. Look at it once again. Are you sure you are doing what the equation demands ?
Maybe you need to look a bit harder at the code I posted. It takes care of changing the flag variable. Look out for the part posted in bold...
All famous compilers come with a host of features and some kind of IDE. If minimalism is what you want, you can go for command line compilers but they just end up sucking your blood if you haven't used them before.
Keep a flag variable which will decide whether the operation should be addition or subtraction.
{
double summand = 1;
int n = 1;
double sum = 1;
bool flag = true;
do
{
summand = summand * x / n;
[B] if (flag)
sum += summand;
else
sum -= summand;
flag = !flag;[/B]
n--;
}
while (summand > 0.00001);
return sum;
}
Some points:
1. Use code tags to post your indented code so that it is more readable.
2. The loop you use for input can easily cause buffer overflows if zero is not entered by the user. If you know the array size in advance, replace all the cumbersome while
loops with the for
loops and repost your updated code.
3. You did a mistake in placing the logic for finding out the highest in the else
block. Place it in a seperate if
block.
4. Initializing your variables will save you a lot of head banging in the future. Always initialize your variables before using them in such situations.
Bumping your posts would do you no good. It would only result in a longer delay and is not encouraged. Patience wins the best help.
I dont get it , what do you mean by "diffrent variables to keep track of the indexes" ?can you give me an example ?
thanks.
You just have to make sure that the element you have to remove doesn't make it into the new array.
This depends on the way you want the removal to happen. For eg. you can do position based removal or value based removal.
And btw, are you sure you have to create a new array? Normally these kinds of exercises require you to make changes in the original array itself using realloc
.
In Rome, do the way Romans do...
If someone wants you to do a project in WinAPI, you got no choice but to do in WinAPI or leave the project. It all depends on the client requirement because its the business requirement which drives the software development arena.
It is actually a good thing to have different proficiencies under your belt. The more the better...
Arun, you need to properly format your code. Something like this:
void read_data()
{
int i,j,any;
char yesno;
char fname[15];
FILE *fp;
clrscr();
gotoxy(10,10);
printf("Data available in disk file Y/N ");
scanf("%c", &yesno);
if(toupper(yesno)=='Y')
{
clrscr();
gotoxy(10,10);
printf("File name to read data ");
scanf("%s",fname);
fp=fopen(fname,"r");
clrscr();
fscanf(fp,"%d",&any);
for(i=1;i<=n;i++)
for(j=1;j=n;j++)
{
fscanf(fp,"%d",&any);
x[i] [j]=any;
}
}
else
{
clrscr();
gotoxy(10,3);
printf("Size of the matrix");
scanf("%d",&n);
printf("\nEnter the matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&any);
x[i][j]=any;
}
clrscr();
gotoxy(10,10);
getchar();
printf("Store data in disk file Y/N");
scanf("%c",&yesno);
if(toupper(yesno)=='Y')
{
gotoxy(10,12);
printf("File name to store data");
scanf("%s",fname);
fp=fopen(fname,"w");
fprintf(fp,"%3d",n);
for(i=1;i<=n;i++)
{
fprintf(fp,"\n");
for(j=1;j<=n;j++)
fprintf(fp,"%5d",x[i][j]);
}
}
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
y[i][j];
}
Now compare the code posted by me with the code you posted. Isn't the difference obvious ?
Assuming that the condition is "The loop should only be executed again (since its a do while loop) when the first is less than second AND the third is not equal to fourth OR when the number of iterations is still less than 5", you should have something like this:
// will be executed atleast once
do
{
// something
}
while ((first < second && third != fourth) || (iterations < 5)) ;
Correct use of parantheses makes the expression logically correct as well as more readable.
Your problem is that the newline character ( '\n'
) which remains in the input stream after you give the choice (yes or no) is picked up by the getline
function and hence your program fails to run as expected.
Either place a getchar ()
after the cin >> response
statement or resort to better methods of accepting input from the user.
And btw, using system ("pause")
is not good choice for making the console window stop, better use getchar ()
again or cin.get ()
.
The n / 2 does not aid in checking for prime numbers. Remove it and you still would get the same answer. It just helps in cutting down extra processing and iterations.
Also read my first post for a better optimization.
You can save iterations and the additional checking performed in the loops by doing something like:
#include <iostream>
int main ()
{
using namespace std ;
int limit = -1, count = 1 ;
bool isPrime = true ;
cout << "Enter the limit: " ;
cin >> limit ;
getchar () ;
cout << 2 << endl ; // handle special case since 2 is the only even prime number
// the number of loops are cut in half by the use of i+=2 since
// only odd numbers can be prime ( except for 2 which is even )
for (int i = 3; i < limit; i+=2)
{
// there is a theorem which states that a number if not prime
// has to have factors less than equal to its own square root
for (int j = 2; j * j <= i; ++j)
{
if (i % j == 0)
{
isPrime = false ; // since factor found hence not prime
break ;
}
}
if (isPrime)
{
++count ;
cout << i << endl ;
}
else
{
isPrime = true ; // reset the flag for next number
}
}
cout << "The number of random numbers in the given range are : " << count ;
getchar () ;
return 0;
}
A simpler way would be to convert each and everything to intger and do away with the floating point confusion and havoc.
Penny - 0.01
Scaled Penny - 1 ( scaled by factor of 100 )
And in the same way, you scale the amount entered by the user and your calculations would be made simpler.
Also if you want an equivalent of modulus operator for floats look for [search]fmod[/search] but keep in mind that it operates only with floating point numbers.
It is because the moment you put parameters, it becomes a function declaration rather than an object creation statement.
Because that is the way multiple assignments ought to be done in C or as a matter of C++. The syntax dictates it. Try doing multiple assignments like Lazaro's on a decent compiler and you end up getting your share of errors.
You need to tell the compiler that the class you are using exists. You do this by something known as forward declarations.
// Enemy.h
#ifndef ENEMY_H
#define ENEMY_H
class Player ; // forward declaration
class Enemy
{
int c ;
int d ;
public:
int get (Player a) ; // now this works
} ;
#endif
// Player.h
#ifndef PLAYER_H
#define PLAYER_H
class Enemy ; // forward declaration
class Player
{
int a ;
int b ;
public :
int get (Enemy a) ; // now this works
} ;
#endif
// Driver.cpp
#include "a.h"
#include "b.h"
int main()
{
Enemy e ;
Player p ;
return 0;
}
You are getting an error because you are trying to print the "input stream" and not the "character array". In your use of fgets ( ) , "input" is the stream from which you are reading, line by line.
char buffer [BUFSIZ] = { '\0' } ;
while ( fgets (buffer, BUFSIZ, file_stream) != NULL )
{
// process the line keeping in mind that it
// has a newline at its end
printf ("%s", buffer) ;
}
s.o.s, Ancient Dragon; thank you for the advice.
You are welcome. Oh and btw, best of luck with your project...
As far as virtual functions are concerned, they definately incur some performance overhead, but in normal senarios, the cost and nightmares of maintaining repeatitive code is far more than that when virtual functions are used.
The most important point you have to consider while using virtuals is that the binding is done at runtime as compared to when the code is repeated, in which case the binding is compile time.
Also a very important thing which mars the performance of virtual functions is that they can't be inlined. So if you are repeatedly calling chunks of code with the virtual tag attached, you suffer a great performance hit.
Then again, it boils down to what kind of design you have used in your project, but still the cost of setting up vptr's and not having the ability to inline has to be taken in consideration.