but my ans is always "password is wrong"
Print both strings and eyeball them:
cout << ">" << passFromFile << "<\n";
cout << ">" << passFromUser << "<\n";
There's probably whitespace or something that makes the difference.
but my ans is always "password is wrong"
Print both strings and eyeball them:
cout << ">" << passFromFile << "<\n";
cout << ">" << passFromUser << "<\n";
There's probably whitespace or something that makes the difference.
What's wrong with what you have? I mean, you're not going to find anything a lot shorter.
How often are pointers to functions used?
As often as they're needed?
Should i implement them in my programs?
Function pointers work great for callbacks and generalized behavior at runtime. If you're doing something where they can be used and they're the best choice, you should use them. What you shouldn't do is look for places to use a random cool feature you just learned. That results in bad designs.
You are passing the address of n and c. That means nextprime should expect pointers:
int nextprime(int *n, int *c);
Then inside nextprime, n and c are already pointers so you need to dereference them to get to the value. n and c gives you the address, *n and *c gives you the value at that address.
You can almost fix things inside nextprime by changing the &'s to *'s, but because of precedence *n++
and other expressions like it won't do what you want. Instead of incrementing the value at the address pointed to by n, it increments the address of n and evaluates to the value at that new address.
To get the right precedence you need to use parentheses: (*n)++;
. Dereference n, then increment that value. Everything you have in main is perfect, but a '\n' at the very end couldn't hurt. ;)
#include <stdio.h>
int nextprime(int *n, int *c);
int main() {
int n = 0, c = 0;
printf("%d, ", nextprime(&n, &c));
printf("%d, ", nextprime(&n, &c));
printf("%d, ", nextprime(&n, &c));
printf("%d\n", nextprime(&n, &c));
return 0;
}
int nextprime(int *n, int *c) {
/* This function will return the next prime number. Simple set n and c at 0 and
call the function whenever you need the next prime number. */
if(*n == 0) {
(*n)++;
return 2;
}
if(*c == 0) {
(*c)++;
return (6*(*n))-1;
}
if(*c == 1) {
(*n)++;
(*c)--;
return (6*(*n))+1;
}
}
Good luck and thanks!
I think it would be better to show your code with the pointer stuff so that someone can help you understand where you went wrong than to show your code without the pointer stuff and hope somebody does all of your work for you.
You don't always have to store the actual value the way float and double do. If the problem allows it, you can write your own data type that uses exponent representation just like you showed in your post:
typedef struct
{
long base;
long exponent;
} exponent_t;
Then do math on it like you learned in grade school. :)
If you specify a value, it overrides the rule. If you don't specify a value, it defaults to one more than the previous constant. If you don't specify any value for the first constant, it starts at 0:
enum
{
A, // A == 0
B, // B == 1
C=10, // C == 10
D=20, // D == 20
E, // E == 21
F // F == 22
};
It helps to know what you expected. I'll guess that you wanted -25.0. Here's why you get -100.0. a=2*(s-u*t)/squ(t);
uses the squ macro, and squ replaces itself with the macro text x*x
with x replaced by the argument t. Your statement looks like this after the replacement:
a=2*(s-u*t)/t*t;
* and / have the same precedence and are evaluated left to right. If you wanted -25.0 as a result, x*x has to be surrounded by parentheses:
a=2*(s-u*t)/(t*t);
That's why it's a good habit to surround your macro text expressions with parentheses. And for the same reason, the macro parameter:
#define squ(x) ((x)*(x))
You should be very careful with macros like this because if you pass an expression with side effects as the argument, the side effect will be evaluated twice:
int b = 1;
printf("%d\n", squ(b++)); /* squ(b++) replaced with ((b++)*(b++)) */
printf(“result :%f”,a);
If you don't print a '\n' character somewhere, the output might look weird.
You have a copy constructor, but it doesn't copy anything.
I can't say what the problem is with what you've shown, but the list makes a copy of the object. Check your copy constructor for the Aircraft class.
I think a better way to go about it is to change your code to do it the Linux way. You shouldn't be using system anyway because it is bad for security.
Show which commands you're using from DOS and I can help you figure out how to do it in Linux without emulating DOS.
That's more helpful, but a format specification would be better. Show an example of the input and output of the program and explain how the input got to the output.
Then it doesn't gather numbers correctly but stores the entire string
That's what you tell it to do with getline(cin,problems)
. I can help you fix it by taking the entire line and parsing it, but I need you to describe the format of the problemset. Just showing your code isn't enough.
It already prompts for enter again. I'm going to guess that the problem is with extra stuff being stuck in the stream when you prompt again. You should clear it out. A good way to do that is to use strings and getline so that there isn't ever any extra stuff. Here's your code with that change, but I removed the timer parts because I don't have your header:
#include <iostream>
#include <string>
using namespace std;
int main()
{
bool quit = false;
string line;
while(! quit)
{
cout<<"hit enter when you want to stop timer\n";
if(getline(cin, line) && line.empty())
{
cout<<"enter yes if you want to find out time differences for more times\n";
if(getline(cin, line) && line == "yes")
{
cout<<"from yes\n";
}
else
{
cout<<"from no\n";
quit=false;
break;
}
}
}
return 0;
}
What I had been using till now was "rusted, naff,useless" code.
There's nothing wrong with writing code on an old compiler. You gotta do what you gotta do, and what you gotta do isn't ever ideal. ;) It's easy for some armchair critic to complain, but you're the one who has to get the job done by any means necessary.
If anyone could help me and provide me with link to the better tutorials on Standard and modern C++, I would be highly grateful.
These two books will help.
From my information, conio.h is not in use anymore.
conio.h is used a lot. There's not a standard way to do the stuff conio.h does either. Clearing the screen, changing text color, getting a key press without typing Enter, standard C++ doesn't do any of it the same way, and the standard analogs are not as useful. If you need something from conio.h, no matter how you go about it you'll get yelled at by somebody that it isn't portable, and your code is wrong, and standard C++ is GOD, and you're stupid for not writing code exactly like them, and whatever else those guys whine about. ;)
I say you should learn how to use everything, portable or not, standard or not, and then choose the best of them for what you need to do. Being standard and portable is a great goal, but to get anything substantial done you have to compromise somewhere.
Ok, that's understandable. You can still do the same thing with a temporary array and an initializer, then a loop to assign the temporary array to the array field:
#include <iostream.h>
class suitcase
{
long double amount[20];
public:
suitcase();
void print()
{
int i;
for (i = 0; i < 20; i++)
cout << amount[i] << '\n';
}
};
suitcase::suitcase()
{
long double temp[20] =
{
0.25,1.0,10.0,100.0,250.0,500.0,1000.0,5000.0,10000.0,50000.0,
100000.0,200000.0,300000.0,400000.0,500000.0,1000000.0,
2500000.0,5000000.0,7500000.0,10000000.0
};
int x;
for (x = 0; x < 20; ++x)
amount[x] = temp[x];
}
int main()
{
suitcase s1;
s1.print();
return 0;
}
"cout" doesn't support any specific formatting.
cout supports a lot of formatting options, and you can add more. It is a very flexible design, much more flexible than printf. But the design is very verbose and convoluted compared to printf.
You can use printf defined under stdio. Use a corresponding qualifier.
I agree. For most of the formatting jobs that printf can do, it is shorter and probably faster that cout. Which makes more sense here when printing fixed format with no precision?
#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;
int main()
{
long double value = 100000000.0;
// chained modifiers on cout
cout << fixed << setprecision(0) << value << '\n';
// printf with a format string
printf("%.0f\n", value);
}
The cout line is a little over twice as long, and it is not as obvious what the output will look like by skimming the code. I think formatting macros embedded in an output string are easier to figure out, but not having to type all of that extra junk is a big win.
If you switch to standard C++ instead of an older version, you can use the STL vector class and initialize it with an array. The Boost library has a way of doing it directly to a vector without the array, and C++0x will add that into the language itself. But this works without Boost or waiting for the next standard to get implemented. It's a little shorter:
#include <iostream>
#include <vector>
using namespace std;
class suitcase
{
vector<long double> amount;
public:
suitcase();
void print()
{
for (int i = 0; i < amount.size(); i++)
cout << amount[i] << '\n';
}
};
suitcase::suitcase()
{
long double temp[20] =
{
0.25,1.0,10.0,100.0,250.0,500.0,1000.0,5000.0,10000.0,50000.0,
100000.0,200000.0,300000.0,400000.0,500000.0,1000000.0,
2500000.0,5000000.0,7500000.0,10000000.0
};
amount = vector<long double>(temp, temp + 20);
}
int main()
{
suitcase s1;
s1.print();
}
If the replacement lines aren't exactly the same length as the lines being replaced, you need to rewrite the whole file. If they are the same length you can just overwrite the first ten lines.
SetConsoleTextAttribute can change the foreground and background:
#include <iostream>
#include <windows.h>
using namespace std;
HANDLE SetColor(
HANDLE console,
int fg = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
int bg = 0)
{
if (console == NULL)
console = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(console, fg | bg);
return console;
}
int main()
{
HANDLE console = SetColor(NULL, FOREGROUND_RED, BACKGROUND_GREEN);
cout << "Hello world!\n";
SetColor(console);
cout << "All done.\n";
}
and I'd also be glad to know another way to clear the screen
Here is a windows way:
void clear_screen()
{
DWORD n, size;
COORD coord = {0};
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(h, &csbi);
size = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(h, TEXT(' '), size, coord, &n);
GetConsoleScreenBufferInfo(h, &csbi);
FillConsoleOutputAttribute(h, csbi.wAttributes, size, coord, &n);
SetConsoleCursorPosition(h, coord);
}
I thought you were from USA, what nationality are you?
Military service in the US hasn't always been voluntary. The draft only stopped around 1980.
I don't understand what the 0 and -5 thing is with that code, but your player's hit points don't ever get subtracted from. If you do player2-hit;
, it doesn't change player2. You need to do player2=player2-hit;
to save the changes. Or player2-=hit
is a little shorter and does the same thing.
I'd also move the check for player1 and player2 being less than 0 to outside the attack2 if statement. But because you print the hitpoints before checking for a game over, it will still print negative values. You can fix that by only printing positive hitpoints. If the hitpoints are below 0, print 0.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace std;
int positive(int value)
{
return value >= 0 ? value : 0;
}
int main ()
{
srand(time(0));
int player1 = 100;
int player2 = 100;
char player1Name;
char player2Name;
int PlayerTurn(1);
int Attack;
bool GameOver(false);
bool GameWin(false);
do {
cout << "it's player" << PlayerTurn << "'s turn" << "do you want to attack with a high risk or a low risk attack? low risk = type1 high risk = type2" << endl;
cout << "Attck with type >>";
cin >> Attack;
if(Attack == 1) {
double hit = rand() % 31 + 10;
if(PlayerTurn == 1){
player2-=hit;
cout << "you took " << hit << " points from player2" << endl;
cout << "\nplayer2 has " << positive(player2) << " points left\n" << endl;
}
else {
player1-=hit;
cout …
There's a line between "help" and "spoon-feeding the complete answer".
If I cross the line then one of the moderators will let me know, but I'll keep those links in mind when I post.
With that solution increase your buffer size slightly
It should be no less than the largest expected line length. I picked 20 because none of the lines in the example file were longer than that.
don't forget your fclose()
You don't need to fclose() if the program ends. When the program ends normally, all open files are flushed and closed.
Also note "rt" on the fopen() if you need your line terminator translation.
"r" is already text mode where whatever line terminator combination is used on the OS, it becomes '\n' inside your program. "rb" is binary mode, and those translations aren't made. "rt" isn't portable.
Use recursion:
#include <stdio.h>
void PrintFileBackward(FILE *fp);
int main()
{
FILE *fp = fopen("test.dat", "r");
PrintFileBackward(fp);
return 0;
}
void PrintFileBackward(FILE *fp)
{
char s[20];
if (!fgets(s, 20, fp)) return;
PrintFileBackward(fp);
fputs(s, stdout);
}
Each time you call PrintFileBackward, the next line is read from the file. Then when the functions are rolling back up the stack, each line is printed. But because lines are printed from the inside out in the opposite order that they were read, they get printed in reverse.
Cool, huh?