tux4life 2,072 Postaholic

You could make use of the following if-statements:

  • Hexadecimal: if(('0'<=c && c<='9') || ('a'<=c && c<='f'))
  • Decimal: if(('0'<=c && c<='9'))
  • Octal: if(('0'<=c && c<='8'))

Edit:: 'c' is a character variable ( char ) of course :P

Just put them in a loop and evaluate each string character by character before doing the actual conversion :)

P.S.: Sorry if my explanation is bad, my native language is Dutch so my English vocabulary is a bit limited ...

Sturm commented: That's horrible. -1
Ancient Dragon commented: cancel out undeserved bad rep +36
tux4life 2,072 Postaholic

To check whether a character contains a valid hexadecimal token (0-9, a-f) you could simply make use of the following if-statement : if(('0'<=c && c<='9') || ('a'<=c && c<='f')) :)

tux4life 2,072 Postaholic

I think you're already on a good way: you've already the skeleton of a class which will contain a flight's details ...

  • Continue creating your Flight class
  • If your Flight class is ready, you could create a vector of it, that's a lot easier as you don't have to bother with dynamic memory allocation, you can also delete/add records in an easy way :) !
tux4life 2,072 Postaholic

>Can you help me with what I am doing wrong?
Sure, what's your problem? (be specific!) Are you getting unexpected output?

tux4life 2,072 Postaholic

>And that makes a difference, how?
I'm sorry but I don't get that question :(

tux4life 2,072 Postaholic

Hey can you explain me of converting a string of char containing a hex notation of a number to a int?

Take a look at this :)

tux4life 2,072 Postaholic

Narue, you're right but I provided him with this trick because his function was returning an integer value ...

tux4life 2,072 Postaholic

Well True,
I pretty much dont know whether I can use stringstreams to solve this exercise.

You can: if you convert the C++ string back to a C-string after the conversion, but that was probably not the intention of the assignment :)

tux4life 2,072 Postaholic

I also need advice on converting a C++ Octal or Hexadecimal Notationed string to an integer.

To do conversions between numbers and strings, stringstreams are often used :)

Lines 28-50 can be replaced by: return a-'0';

tux4life 2,072 Postaholic

The first method that comes up with me is the following one where a command's/program's output is saved in a file ...

Consider the following problem: you want to display the output of the ls command in your SDL program, then you just add the following command to your program: system("ls>output.txt"); The > lets the shell write the output of the ls-command to a file called 'output.txt', now the only thing left to you is read this file into your SDL Application and display it's contents on the screen :) ...

Hope this helps !

edit:: This is probably not the best method to do this, but it's a simple one which works perfectly if you just want to save a programs output to a file for viewing/displaying it later on ...

tux4life 2,072 Postaholic

Hello again all!!! I just have two quick questions...

1.) Is it possible for one to run a program through the "system(command)" and have the output come out on top of an SDL program? Like if I had a background of some sort and typed "system(echo "Hello!")" could I have that output appear ON TOP of the background and not in the terminal itself?

Yes, this should be possible but user interaction will be more difficult I think, I have never done this before, but with the system command you just invoke a new shell and run the specified command in it, if I'm not wrong the concept you're looking for is called piping , so use google !!! :) ...

tux4life 2,072 Postaholic

The first question you should ask yourself is: is it worth all the work to built in all those code to create a demo version?

What's your application about, what does it do?

You should also think about the following problem: If the trial/demo period has ended, the user could just set his computer's clock back in time :), you should also make this impossible to do ...

tux4life 2,072 Postaholic

According to this specifications it isn't ...

Well, I forgot to mention you should click Full specs :)

tux4life 2,072 Postaholic

Use cout << n << " "; instead of cout << setw(5) << n << endl; Right after the nested for-loop (line 17) you've put the following instruction: cout << endl; The problem should be fixed now :)

tux4life 2,072 Postaholic

Another remark: avoid the use of system("pause"); (See http://www.gidnetwork.com/b-61.html)

You could use cin.get(); instead of system("pause"); :)

tux4life 2,072 Postaholic

A bad_alloc exception is thrown when dynamic memory allocation has failed, I think this is often the case when your system has not enough memory, or are you allocating to much memory ?

A memory leak could be, but me eyesight is not good enough to see your program's source code from here :P

tux4life 2,072 Postaholic

When I put #include "Singleton.cpp" in SingletonMain.cpp it compiles. This is confusing me . .

That's because the compiler is just replacing the #include directive with the whole contents of Singleton.cpp , so the compiler's pre-processor threats it as a header file :)

tux4life 2,072 Postaholic

Why are you using a .C file-extension for SingletonMain and why a .cpp file-extension for the file Singleton ???

I would just use .cpp for both file's extensions, as it's the most common :) ...

tux4life 2,072 Postaholic

Actually you should check the input on being a valid integer or not :)

To check whether the input is an integer or not, you could do something like this (you'll have to pass the input as a string):

bool checkInt(const string &s)
{
	int len = s.length();
	for(int i=0; i<len; i++)
		if(!isdigit(s[i])) return false;
	return true;
}

TIP: Read your input (the number/integer) as a string, pass it to this function, if this function returns true , convert the number to an integer (you could use stringstreams ) :)

tux4life 2,072 Postaholic

Did you run scandisk command on your computer?

In Windows XP it isn't called scandisk but chkdsk :) ...

tux4life 2,072 Postaholic

Reading needs to be done immediately before the test with .eof() .

But if you use a do-while statement, the eof() check is always performed after the reading, so what's wrong with it ?

tux4life 2,072 Postaholic

That's now what happens, if you don't post using code tags you get smilies in your code which make it a mess ...

tux4life 2,072 Postaholic

But, I checked it in a C++ Book(*) before I posted these examples ...

(*) C++ Beginner's Guide by Herb Schildt, a free legal ebook copy is available here, so you can check the do-while if you want (Chapter 11, p31-p33) ...

tux4life 2,072 Postaholic

Avoid using system("cls") (look here for why, the reasons are the same) :)

tux4life 2,072 Postaholic

Please do some effort first !

tux4life 2,072 Postaholic

According to this specifications it isn't ...

tux4life 2,072 Postaholic

Why using dynamic memory allocation?
You could simply do something like this:

void reverse(char p[])
{
	int len=strlen(p);
	char t;
	for(int i=(--len), j=0; i>len/2; i--, j++)
	{
		// exchange elements
		t=p[i];
		p[i]=p[j];
		p[j]=t;
	}
}

You cannot make mistakes (in the use of pointers :P) if you use this approach ...

tux4life 2,072 Postaholic

>For some reason the tag isnt working(OFFTOPIC) Did you type the word 'code' in uppercase ?[code=c++] tag isnt working(OFFTOPIC)
Did you type the word 'code' in uppercase ?

tux4life 2,072 Postaholic

I still donot understand the reason of not to use eof(); Please Explain a lil more :)

I didn't say you aren't allowed to use it anymore, you just have to make sure that you first read something from a file before using the eof() function, it might be advisable that you put all the instructions which are reading/writing from/to a file into a try-block so exceptions will be caught :) ...

If you really want to use a loop do something like this:

string x;
[B]infile>>x;[/B]
while(!infile.eof())
{
     infile>>x;
     /* your other instructions here */
}

or use a do-while statement:

string x;
do
{
     infile>>x;
     /* your other instructions here */
} while(!infile.eof());

or

string x;
while(infile>>x;)
{
     /* your instructions here */
}
tux4life 2,072 Postaholic

well i have compiled it and its giving me the output.....

i work on code blocks and i think its mingw compiler.

Sorry, I was wrong about that, but I didn't take into account you were using compiler extensions ...

And Narue has proven me wrong again :P !!

Edit:: I checked it and using the MinGW compiler it compiled perfectly :)

tux4life 2,072 Postaholic

>but if you'd like that I don't do it anymore just say it
I don't dictate how people should post, I have my own quirks as well. ;)

Yeah, but if it's confusing I'll try to avoid doing this ...

tux4life 2,072 Postaholic

well i just compiled this and it worked...

LOL, that's impossible !
What ancient crappy old compiler are you using?

A standard C++ compiler (this is a compiler which complies to the ANSI/ISO C++ standard will never compile this)

The evidence: I compiled it using the Borland Compiler this is the output:

Borland C++ 5.82 for Win32 Copyright (c) 1993, 2005 Borland
brol.cpp:
Error E2313 brol.cpp 32: Constant expression required in function main()
*** 1 errors in Compile ***
tux4life 2,072 Postaholic

Dear rahul, your code won't compile: student std[x]; is never going to work :P (edit:: unless x is a constant expression)

Thats just what the OP was asking for: Dynamic Memory allocation, your code doesn't explain dynamic memory allocation I think ...

Edit:: Take a look at this to get a basic idea about dynamic memory, you can easily adapt it for structures :)

tux4life 2,072 Postaholic

Hey tux, why do you use a > prefix with replies? Generally it's used for quotes, which makes your posts somewhat difficult to decipher.

I'm just using it to distinguish different steps, but if you'd like that I don't do it anymore just say it :) ...

tux4life 2,072 Postaholic

> Ask specific questions about what you don't understand, all what you've done is just copied and pasted your assignment in this thread :) ...

> If you want help, do some effort first, give it a try, and when you get stuck you can always ask questions ...

tux4life 2,072 Postaholic

> The eof() function will tell you whether an attempt has been made to read past the end of available data, not whether the next read will fail due to lack of data.

Never write code that looks like: while (!infile.eof()) { read_some_more(); } > I hope this is explaining why skydiploma's code was bad ...

edit> Take a look here for some more information on the eof() function :) ...

tux4life 2,072 Postaholic

> You could also adapt the technique described in this post :)

tux4life 2,072 Postaholic

> I'm writing exactly the same code, but probably a bit easier to understand:

>> Original posted code:

int sum=0, a=0, b=0;
for (a=1;a<3;a++)
for (b=1;b<5;b++)
sum+=b;
cout <<"sum =" <<sum;

>> The above code is actually pretty much the same as this :):

int sum=0, a=0, b=0;
for (a=1;a<3;a++)
     for (b=1;b<5;b++)
          sum+=b;
cout <<"sum =" <<sum;

>>> Line 1: int sum=0, a=0, b=0; declare variables (variables are used to store data in, a variable is actually a name (assigned by the programmer) for a place in the computer's memory

>>> Line 2: for (a=1;a<3;a++) , a for-statement which is executing three times the following instruction: for (b=1;b<5;b++) (another for-statement), a for-statement is called a 'loop' which means it's repeating instructions until a certain condition isn't true anymore ...

>>> Line 3: sum+=b; , this means the same as sum = sum + b; which means that the variable 'sum' is incremented by the value of variable 'b' each time ...

>>> Line 4 (the last instruction): cout <<"sum =" <<sum; , cout is an object from the C++ Class Library and is used to display things on the screen, when you run this code, the output you see on your screen is what this line of code did ...

>>> You can find some more information on the for-loop here ...

> Hope this helps :P !

> By the way, what don't you understand? We can't give …

tux4life 2,072 Postaholic

Thanks it works!!

> From the comments skydiploma got on his post I read the following: "Find the bug. - Dave Sinkula"

>> Now to the OP: Welcome to the world of C++: it isn't because it seems to work that it actually works :)

tux4life 2,072 Postaholic

> if (negitive == false) you can also just write if (!negitive) > The following code is not fully correct:

double z = sqrt(x);
negitive = true;

if (negitive == false)
{
	z = abs(x);
	negitive = true;
}

>> You first have to check whether the number is positive or negative, before calculating the square root of it:

>>> So put the following code in your function before double z = sqrt(x);

if(x<0)
{
     // It's a negative number
     x = -x; // Turn the negative number into a positive one
     negitive = true;
}

>> Before you calculate the square root of a number you should ensure yourself that it isn't negative, so double z = sqrt(x); becomes double z = sqrt(abs(x)); >> At the end of the function you just put the following instruction: return negitive?-z:z;

tux4life 2,072 Postaholic

But I don't know why it is being set to

> Did you actually write this code yourself?

> If yes, you could try using a debugger

> BTW, Avoid the use of system("PAUSE"); (look at this excellent information written by WaltP :))

tux4life 2,072 Postaholic

> Have you already read this ?

tux4life 2,072 Postaholic

it happens to everyone. i covered for you.

And if Ancient Dragon is also doing his contribution it will be complete :P LOL ...

tux4life 2,072 Postaholic

> Take a look at this
> Try this one also :P

tux4life 2,072 Postaholic

You're right jephthah, but the problem is that I gave him wrong rep, it was my fault, I was to fast :angry: ...

tux4life 2,072 Postaholic
#include <al[B]b[/B]orithm>
#include <string>
using namespace std;

int main()
{
   string str = "hello world";
   transform(str.begin(), str.end(), str.begin(), [B]tupper[/B]);
}

> I think you've made some typos:
> #include <alborithm> I don't know any 'alborithm' lol :P
> I even don't know any 'tupper' :P

tux4life 2,072 Postaholic

i've tried already but still can't. any kind soul to advise on my codes?

> If you post just use the button with the sharp: '#' and it will generate code tags for you, now you just have to paste your code between them, is that difficult? NO!

tux4life 2,072 Postaholic

The logic behind the problem(which is described) is rubbish.

If the user entered number is negative, the problem is should be dealt with the concept of Complex Numbers.

I think you have misunderstood the problem. 'Cause no fool will give an assignment to sum the squareroot of two negative numbers and ask you to omit the negative sign while summing it.

> I know but that's his assignment, probably because the teacher wants him to use a control structure like the if-statement ...

tux4life 2,072 Postaholic

> If you've other questions (regarding this assignment) please feel free to ask !

tux4life 2,072 Postaholic

A quick revision:

> Make the function returning a variable of type double (just replace the void keyword by double ) ...

> You'll have to return variable z 's value, you can do this using the instruction return , e.g.: return z; > Check the number before you calculate the square root of it, if the number is negative you have to make it positive and then you'll have to calculate the square root of it, you can do this as follows:

>> First you check whether the number is positive or negative (you store this in a bool variable called negative , if the number passed to the function is negative you set the boolean variable negative to true, (in the declaration you set it to false by default), you take the absolute value of the number where you want to calculate the square root of and at the end of the function (just before it's returning it's value, you check whether you'll have to return the negative/positive of the square root (use the bool variable negative !)

I hope this explanation helps you to solve this problem :) !