Bench 212 Posting Pro

your line number will be the position in the list, so you probably want to implement a function which will iterate through the list 'N' number of times to return a pointer to the element you want (or return a null pointer if the line at N doesn't exist)

Bench 212 Posting Pro

I assume the main function is that one called main() - which is the entry point of your program

If a variable is global, that means its declared outside of any functions. (the terms "local" and "global" refer to where in your program that variable is declared. the syntax for declaring the variable is otherwise the same)

Bench 212 Posting Pro

How about something like Hangman or Noughts & Crosses?

Bench 212 Posting Pro

Why have you created your own isspace function? You'd be better off using the one declared in <cctype>

Bench 212 Posting Pro

As a starting point I also have written the following code, but it errors on me.

if (toupper(temp.at(0) == 'E') break;

You missed a bracket out ...

if (toupper(temp.at(0)[b])[/b] == 'E') break;

for some reason when it goes through the first time it will not stop and take a prompt, it just plows right on through with and empty string. However, when it loops back through it will stop and take a prompt. That seems strange to me. Any ideas on that one?

You probably have a newline character left in cin somewhere.
Read my (edited) post above on that subject.

Bench 212 Posting Pro

if you want to retrieve an entire line from a stream (such as cin, or a file), then you need the getline function

string temp;
 getline( cin, temp );

Edit- by the way, if you use cin >> you need to be careful, because it comes with a few pitfalls, such as leaving newlines in the stream, and needing to be checked for errors.

A better way is to always read your input as a string using getline and convert from a string to a number using stringstream's

you may want to try something like this

//Retrieve an int from an input stream
int get_int( istream& is )
{
    string input;
    int output;
    while (true)
    {
        //Read a whole line of text from the input stream
        getline( is, input );
        stringstream ss(input);

        //If the input is an int, break out of the loop
        if( ss >> output )
            break;
        else
            cout << "Error\n";
    }
    return output;
}

int main() 
{
    cout << "enter a number: ";

    //Get an int from 'cin' - No need to worry about error flags or rogue newline's
    cout << get_int( cin );
}
Bench 212 Posting Pro

the problem might be easier for you to see if you used a more consistant indenting style.

Here it is, reformatted (Courtesy of MSVC++'s auto-format tool):

int main() 
{
    clrscr();
    int one, two, three,x;
    char choice;
    while (choice != 'e')
    {
        cout<<"\nWELCOME! To access my calculator, please follow the instructions carefully.";
        cout<<"\n\n\n";
        cout<<"\nPlease enter the correct number code: ";
        cin>>x;
        if (x!=123)
        {
            cout<<"\n";
        }
        else
        {
            cout<<"\nYou may now use the calculator!";
            break;
        }
    }

    cout<<"\nPlease enter +,-,*, or / and then two numbers,\nsepperated by spaces, that you wish to\nadd,subtract,multiply,or divide.\n\nType e and press enter to exit.";
    cin>>choice;
    if (choice != 'e')
    {
        calc(choice);
    }
}

your if (choice != 'e') statement is not inside any kind of loop, so there is nothing to make it repeat.


I'd also like to point out this bit

char choice;
    while (choice != 'e')

your 'choice' variable is uninitialised when you compare it for inequality with 'e', which causes undefined behaviour (anything can happen...)

Bench 212 Posting Pro

That would be the case with nearly any IDE/Compiler though - most that I'm aware of do support nonstandard features or come with a whole load of extensions - the key here is to learn the standard language using a good book to keep you on the straight and narrow. (Rather than worrying about the capabilities of a particular implementation - provided it supports the ISO/ANSI standards of course)

Bench 212 Posting Pro

Would be much more time effective if you would abandon this IDE.

I think he has already abandoned the old, outdated "turbo C" from the early 1990s, and is now using the modern, standards-compliant 'Turbo C++ explorer' ... There's nothing (that i'm aware of) which is wrong with this newer version, although I've never used it myself, the description on the website gives the impression of a good, modern IDE.


To the OP : Have you tried looking at the IDE's website? there may be some guides or useful articles which might get you started there http://www.turboexplorer.com/cpp

Bench 212 Posting Pro

Of course some professionals use goto's occasionally ... with reference to the linux kernel, here's Linus Torvalds' take on goto

http://kerneltrap.org/node/553/2131


Although I would put goto's in the same league as Macros, Unions, Multiple Inheritance, reinterpret_cast, placement new ....

in other words - all things which you should generally avoid unless you really, totally understand what you're doing, where the pitfalls are, and what the alternatives are (And have made a concious decision that the benefits of your chosen technique outweighs the potential problems)

Bench 212 Posting Pro

I ran your code, Bench, but it doesn't do what gaggu82 asked for. It finds the search string even if it's embedded in another word, like "this is a string" prints "is is a string" instead of "is a string". Am I testing it the wrong way?

No, you're not :) I think the original post was unclear to me (It makes more sense now - the thread starter could have done with more punctuation though - the question was bordering on cryptic as I read it)

in which case, i'd add this to the top

bool is_space_punct( std::string::const_iterator iter )
{
    return isspace( *iter ) || ispunct( *iter ) ;
}

and use this for bounds checking (I don't think the STL can help much with this bit)

bool is_word = true;
    if (iter != line_in.begin() )  //Don't check the beginning
        is_word = is_space_punct( iter-1 );

    if ( iter+match.size() != line_in.end() ) //Don't check the end
        is_word = is_word && is_space_punct( iter+match.size() );

Now its fairly similar to yours, but with iterators rather than indices (I tend to find iterators are a bit more idiomatic for C++, probably because of the huge number of things you can do with the STL algorithms) :)

Bench 212 Posting Pro

There might be a way in the STL to do it, but I don't know. I'd write a search function that finds every occurrence of "is" and then checks to see if the first and last letters are on a word boundary.

Yep, the STL is handy here, although your way works fine too.

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    const std::string line_in("the cat sat on the mat");
    const std::string match("cat");
    std::string::const_iterator iter =
        std::search(
            line_in.begin(), 
            line_in.end(), 
            match.begin(), 
            match.end() 
    );

    if (iter != line_in.end() )
        std::cout << std::string(iter, line_in.end() ) << std::endl;
}
Bench 212 Posting Pro

please write the programming code of this reverse contents of array in c++

Please read this link http://www.daniweb.com/forums/announcement8-2.html

Bench 212 Posting Pro

i dont know how to write this code..anyone can help me ?? here are the question :

Write a complete program that calculates the wages of an employee for the month(hint : input from user includes year,month,time in/time out for each day in that month.).

monday-friday :first 8hrs,Rm50......add.hour RM10/hr.
saturday and sunday:RM20/hr

thank you..

Which bits do you know how to write? Try to get as far as you can, and post your attempt. If you're having trouble with the logic of the program, try working out the calculations step-by-step on paper from a human perspective - this might make it easier to think about what steps the program needs to do the same, and what variables & calculations are involved.

Bench 212 Posting Pro

you don't need to take value_type out, just make sure the name is fully qualified when using it outside of the class, ie,

[b]node::[/b]value_type node::get_data() const
Bench 212 Posting Pro

yeah i use struct in C but not in C++ though it supports for struct.

In which case, you're probably aware of how handy it is to be able to group data together in a struct with your own data type. The idea of a class extends the concept of organising data in this way by throwing functions into the mix.

for example, if you've ever created a struct in a 'C' program, you've probably ended up with something like this in a header file

typedef struct
{
    int i;
    char* str;
    int size;
} my_type;

void do_something(my_type*);
void something_else(my_type*);
int another_function(my_type*);

The functions which accept a pointer to my_type are all designed to do something with a my_type object, So why not strengthen that relationship? - this is what classes do!

With a class, you could re-write that bit of C code like this

class my_type
{
    int i;
    char* str;
    int size;
public:
    void do_something();
    void something_else();
    int another_function();
};

The benefit here is that you no longer need to pass a my_type pointer to any of those functions, because each function is directly associated with my_type, and has one-to-one access with other data members of my_type.

classes bring up huge numbers of design issues, which opens up a whole new world of designing programs. These issues are best examined from a high-level OOP/OO-Design perspective (And i strongly reccomend reading up on OO Design) - which introduces ideas that may be unfamiliar to …

SpS commented: Good ~SpS +3
Bench 212 Posting Pro

As with others of this type, it works better spoken than written.

The third word is 'Language', as it occupies the third slot in the phrase 'The' 'English' 'Language'.

ah well, I guess that one was just too easy ;)

I am a devourer.

All that crosses my path will I engulf, though
some meals I am forced to gnaw on for a time
before I finally consume them.

The fickle wind is both my greatest ally and
my greatest foe. The touch of her breath may
give me new life, or may utterly obliterate me.

As the wind is to me, so I am become to mankind.
He may harness my power for himself, and I will
aid him, but should he grow careless, he too
will I devour.

Though they be held back, the stones of the
deepest earth desire to take on my nature.
Should they be freed upon the world, for a
time they too will devour all they touch,
even as I devour.

What am I?

My guess is - Fire

Bench 212 Posting Pro

Hello, i have been reading the chapter in the book about classes and i have learned everything about them the only question i have now is that wut do you use classes for?

Have you ever used the struct keyword to create your own simple type combining several variables together? That's an extremely simple example of a class.

Bench 212 Posting Pro

Here's another of my favourites :)

'Angry' and 'Hungry' are both english words ending in 'gry'. There are three words in the english language, what is the third word?

Everybody knows what it means, and everybody uses it every day, but what is the third word?

Bench 212 Posting Pro

would it be one? the others are met. they could have been met there or even if they were met along the way nothig says that they are going with?

Correct! :) Have a cookie :D

that's just as the other one that worked only when spoken... 'cause when you write it, you can read back...

Yeah, the film uses it as a quickfire 'game' over the phone, giving the main characters 30 seconds to get the right number before a bomb goes off.. (Its not the most realistic film ever made ;) )

None, only the original man going to St. Ives. (I've never seen the movie) And of the things asked, a man is not listed so the answer should be zero.

Well, the question asked is "how many are going to St. Ives?". the kittens/cats/sacks/wives is a red-herring, and not part of the question :)

Bench 212 Posting Pro

Anyone who's ever seen Die Hard With a Vengeance should remember the answer to this one.. If you haven't seen the film, see if you can work it out in 30 seconds ;)

A man was going to St. Ives..
He met a man with seven wives..
Every wife had seven sacks..
Every sack had seven cats..
Every cat had seven kittens..
Kittens, Cats, Sacks, and Wives,
how many were going to St. Ives?

Bench 212 Posting Pro

nvm the errors cleared but the problem is it gives out 32 outputs. (your code) I assume 1-26 = A-Z but what is the other 6?

The output should have been the characters 'A' to 'Z' in octal, 101-132 (that's 26 numbers, not 32). What output did you get? did you run the example exactly as it was shown?

This is what I got when I ran Hamrick's program (Copy & pasted from console output window)

101
102
103
104
105
106
107
110
111
112
113
114
115
116
117
120
121
122
123
124
125
126
127
130
131
132

Also your code isnt very practical to use with the code I presented. I would have to re-engineer your code to put it into my program.

Don't expect people to provide complete copy & paste examples that you can just use without thinking. If you can understand what's going on in the example, it should be a piece of cake for you to change the code to suit your program, otherwise, point out the bits that you're struggling with and someone might be able to guide you further.

Bench 212 Posting Pro

2. You use a pointer, THEN later on test it for NULL

2. Why is it wrong?

NULL means that the pointer doesn't point to anything, so using a pointer and testing it for NULL afterwards is an entirely backwards way of doing things. Consider this scenario at breakfast time -

pour_milk( glass );
if( glass == NULL )
    //Too late, we've already poured the milk, and its gone all over the 
    //  floor, because we didn't check the glass was there before pouring!
if ( glass == NULL )
    //The glass isn't there, better get one out of the cupboard
else
    pour_milk( glass );  //That's better, we know that a glass exists.
Bench 212 Posting Pro

Hi to all, I hope uyou can help with the following situation i have:

I have to write a function that receives a parameter, that is unknow until runtime, meaning one of the parameters could be:
CString, int, double, bool or something else
The function receive additional parameters that are know and one that is unknow,

If you really mean this, then templates can't help you here - the type needs to be known somewhere in your program at compile time in order for the template to be instantiated.

You could think of a templated function as a "meta" function, which doesn't exist unless it is somehow given full type information to fill-in the blanks when the function is used. The compiler automatically generates these variations of the template as seperate functions at compile time.

Perhaps you could shed a little more light on where this function appears in your program, what you want it to do, and how it fits into your design. Also, how does the type affect what the function does?

Bench 212 Posting Pro

woo! right up my street! :D i got 70% .. I guess ALF was an american thing? And i didn't even know the pacman ghosts had names ;) ... also, who's Punky Brewster?

The rest are obvious :D

Bench 212 Posting Pro

Hello, I'm new to the site here. I say this because there seems to be a fair bit of etiquette here that I could step on so if I do, that's why.

Welcome to DaniWeb! :) I reccomend reading the threads at the top of the forum marked Announcement and Read Me if you haven't done so already

Anyhow, I had to take C for my software certificate and since I wanted to take C++ and couldn't, I'm trying to self teach it to myself. I'm doing that by essentially going through my old C book and writing the homework problems in C++ instead so that explains the simplicity of this first question.

C and C++ are actually very different languages, as I'm sure you're finding. If you're serious about C++, then find yourself a good introductory C++ book, which will have excercises that encourage you to solve problems in a C++ way.

1. Ok, I hope I do this right... I have a C problem that asks for you to take in a character from the keyboard. It then wants me to print it twice, once as a character (%c) and then again as an integer (%d) and I can do that without batting an eye. I am not, however so familiar with the cout and cin commands. I was able to reproduce the results by using a cast in the cout statement but I need to know if there is an equivalent of the type specifier …

Bench 212 Posting Pro

25/50 ... although i took random guesses at a third of the questions. :)

This test is not a matter of truth, but of what liberals believe is the truth.

Most of it has nothing to do with politics. They're all based on statistics and quotations as reported in the guardian

Bench 212 Posting Pro

Whats wrong with c/c++? Anything can be taught to anyone at the right pace.

I completely agree with that sentiment, but I don't think its about teaching serious programming to kids, its more about the basic ideas (especially if its at the age where they're still learning multiplication tables).

So, rather than going in-depth, the learning-languages just give a taster of the really simple stuff, with an emphasis on making it easy, fun and creative (Since the average 8 year old generally doesn't have a very wide attention span when they're bored - so if the cute environment helps with motivation, then that's half the battle won)

Bench 212 Posting Pro

You don't need to create the entire weapon class inside the player class, (at the moment it seems to be inside the armour class for some reason. This seems to imply that a weapon is an implementation detail of armour. If you don't want this, you can make it stand-alone).

I believe what seanw is referring to, is something like this

class Weapon
{
    // Weapon stuff
public:
    void do_damage(int amount, string to_what);
};

class Player
{
    Weapon players_weapon;

    //Other player stuff
public:
    void attackit();
};

The idea being that any interaction with players_weapon will take place within methods of the Player class, for example, your attackit method might look something like this

//The attackit method is a member of the Player class, so it has direct access to players_weapon
void Player::attackit()
{
    players_weapon.do_damage(20, "Big_Monster");
    //More attackit stuff
}
Bench 212 Posting Pro

turtle robot with a pen at primary school

hey, we had that at my my primary school - 90's/ :)

Heh, how late into the 90s? Those things weren't exactly cutting edge when I was there (88-92'ish).. then again, the amount of money spent on school computers is pathetic even now, so there's probably more than a few places who still use the things. :)

Bench 212 Posting Pro

To me, it's just another socially unacceptable habit (like smoking). Once widely practised, it is now banned in many places.

Seems like a fairly blunt view of religion you've got there :)
I don't see religion as a problem in itself (We all have to believe something, right?) Afterall, some of our most ancient laws and 'human values' come as a direct result of religion in some form or another.

The problem, as you say, is with brainwashing and extremism, when people of a certain orientation decide that its in their own interest to affect someone who's not of their own beliefs, and to inflict those beliefs or that way of life on someone else.. In this light, the problem is far bigger than just one of religion (for example, rival city gangs fighting between each another, left-wing militants versus right-wing militants etc). The key is mutual understanding, moderation, and acceptance, even if we might not be 100% happy with someone else's view of the world :)


And, err, just to stay on topic, I think if you manage to let programming take up all your waking hours and interfere with some other lifestyle stuff, then there's a real imbalance! :P Personally i'd like to leave at least two or three hours a day for rest & relaxation.

Bench 212 Posting Pro

they used to create Logo for that purpose in the 1980s. It was a complete flop.

I would disagree with that somewhat. As a kid growing up in the 80s, I was exposed to Logo and the turtle robot with a pen at primary school, running on a BBC Microcomputer. I found it entertaining at the time - and remember enjoying the whole experience of being able to type some commands into a computer and watch the turtle roll around the paper on its own.

Of course, I don't think it was in the least bit advanced enough to teach me how to program, but it did capture my interest enough in the idea of writing programs, and maybe some of the ideas about instructions and repetition subliminally 'stuck' too.

So, if this ends up being the next 'Logo' for kids in the next few years, I don't think it'll be a terribly bad thing.

Bench 212 Posting Pro

I wonder if anyone has seen this before - a programming 'language' aimed at young children. Of course, not really a programming language in the usual sense, and certainly not going to produce the next Quake game, but a great idea to introduce people to the basic ideas of programming in a way that lets them do creative and fun stuff without actually knowing how to program. A neat way (IMHO) to capture an audience who otherwise are likely to never do anything more complicated with their computer than read their emails or download something from iTunes.

http://scratch.mit.edu/

What does everyone else think?

Bench 212 Posting Pro

The best suggestion I can offer is to download a few and try them, see which you like the best. choice of IDE is a personal preference, and shouldn't affect the kinds of programs you can create when you're just starting out with the standard language.

in addition to Turbo Explorer and MSVC++, some other popular IDE's available for windows are Dev-C++, and Code Blocks

Bench 212 Posting Pro
void BankAccount::check_input(int dollar,int cent,double rate)
{
  if ((dollar<0)||(cent<0)||(rate<0))
    cout<<"Illigal values !!!";
[B]    exit(1);[/B]
}

Look at this line, and where it is. (Ignore indentation) what happens in this function?

Bench 212 Posting Pro

cin>>"Enter a Date(1)">>endl;


then the user enter 7/03/2007. But i need the year which the user input for other purpose like checking leap year.

That doesn't help. Please post the actual code, and not a mock-up.

Bench 212 Posting Pro

As Ancient Dragon said, there's always something you can learn to improve yourself

For example, another language, different ways of using the language(s) you currently know, design patterns, different libraries/APIs, problem analysis/design, algorithmics, learning other paradigms, machine code.. the list goes on.

Bench 212 Posting Pro

First thing I suggest doing, is looking at this bit of your program carefully

enum SquareState
{
(blank = 's',
X = 'X',
0 ='0')
};

If you can't spot the errors, do some reading about enums

Ancient Dragon commented: absolutely agree with that approach. +15
Bench 212 Posting Pro

That depends - Do you have an example you can post of the code where you're reading input from the user?

Bench 212 Posting Pro

There are, however, firefox extensions that will "double" or even "quadruple" your download speeds.
DownThemAll, is an example.

So they claim :) While i've never tried them, i'm dubious, since I'm well aware of the speeds which i pay for, and, as far as I can tell, most of my downloads run pretty close to those speeds (They also saturate the connection enough that checking my e-mail takes 4 times longer)

Bench 212 Posting Pro

But you can still help me...for future reference, where do I look for NetBIOS as installed or not? If absent, where would I go to 'enable' it?

Glad you got it solved, even if accidentally!

For NetBIOS, look in the advanced TCP/IP settings, on the WINS tab.

The default is "Automatically detect NetBIOS settings" (Which, in real terms usually means its disabled). so switch this option to "Enable NetBIOS over TCP/IP".


There is also an 'old fashioned' way of getting NetBIOS running, by adding the NWLINK IPX/SPX protocol to each machine, and enabling NetBIOS over IPX/SPX. This is usually necessary if you're communicating with a machine running something like Win95 (which doesn't support NetBIOS over TCP/IP for some reason).

Whichever one you do, make sure you use the same on all machines.

Bench 212 Posting Pro

while(month<=12), the loop runs 12 times

Not quite - month never changes - which gives two possibilities:

- Month starts out greater than 12, and the loop doesn't even start
- Month starts out less than or equal to 12, and the loop repeats forever

Bench 212 Posting Pro

Its running non-stop because the same bit of code is repeating all the time that month<=12 is true.

You need to add something into the non-stop-repeating block of code, which changes month, so that eventually the condition becomes false.

Bench 212 Posting Pro

I use Turbo C++, and it always works with <iostream.h>
void main ()

by the way, I think it is caused by error caculation or sth like that.

Thanks

Assuming you mean the MS-DOS based old borland Turbo C++ compiler from the mid-90s (Which pre-dates the language standard, and misses out alot of modern language features), then I reccomend getting a modern compiler.

It would help if you said what the error was, too.

Also, look at this - you have an infinite loop, and a missing double-quote mark(bold)

while (month<=12){
    sum = money + money * 3/100
    cout<<"Total amount ="<<sum;
   [B] cout<<Total interest ="<<sum - money;[/B]
}
Bench 212 Posting Pro

Integers are only capable of representing whole numbers.

Have a look what happens when you do this (Remember that both 3 and 100 are integers)

cout << 3/100 ;

change your variables from type int to type double, and change your 3/100 to double's - ie, 3.0 / 100.0 Also, what compiler are you using? your header files and main declaration are wrong by the C++ standard. If you're on a modern compiler, change it to this -

#include <iostream>  //No ".h" extension for standard headers

//...

int main()  // main always returns an int
{

//...
Bench 212 Posting Pro

Look at this section of code, where you ask the user to enter the number of children

void HowManyChildren(void)
{
	DailyChild=1.50;
	YearlyChild = 5.50;
	
	printf("\n");
	printf("#of Children:   ");
	scanf(" %d", &[B]childrenNum[/B]);
	printf("\n");

	if (childrenNum <= 999) 
	{
	}
	else 
	{
	printf("Invalid Number Entered, please try again.");
	printf("\n\n\n");
	HowManyChildren();
	}

	return;
}

After asking the user to input the number of children, your program doesn't do anything else with childrenNum aside from checking that its less than 999.

Somewhere you want to calculate the ticket cost using this number and your cost-per-ticket. Maybe you intended to put that in your ChildrenCalculations() function.

- With regards to C vs C++ - These days, its unusual for a C++ course to use things like printf, scanf, strcmp (They're valid in C++, but only for the sake of compatibility with C). Most modern C++ courses don't teach the language as 'a better C' - although there's nothing wrong with learning both languages, you'll find alot of things done differently when you move onto C++.

Bench 212 Posting Pro

This isn't C++ code, its C. (The two languages are very different)

Probably the reason that you get an error when your code exits, is that your declaration of main() is ill-formed.
Change your main declaration so that it looks like this -

int main(void)

As for your issue of the wrong output, it looks as if your program doesn't do anything with the number of children after you prompt the user for it.


I think your program has a lot of design issues - you're using alot of global variables, this is one way to make your program very messy and hard to follow. Try rearranging your program so that variables are declared where they're needed, and passed or returned to/from functions instead. You should find that the logic of the program is much easier to trace by doing this.

Bench 212 Posting Pro

there are different, one needs the system("pause"), one do not need because my previous compiler do not need it

You don't need to do that in VS2005. Read my post earlier in the thread.

Bench 212 Posting Pro

Have a look at Beej's guide to network programming. Its geared towards linux, but much of it applies to windows programming too

http://beej.us/guide/bgnet/

Bench 212 Posting Pro

Yes I want to share printer and be able to simply move data/doc/pic files between the two PCs.

Sharing is enabled on both PCs but I can only get access to wifes PC while mine is 'inaccessible'. Using 'Find computer' I can see my PC from her side but cannot access even 'Properties' without the 'accessible' error message

Do you have NetBIOS Over TCP/IP enabled on both computers? NetBIOS is needed for windows file & print sharing.