WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

heyi just wrote and comiled with Dev-C++
----
they said that its meant to come up with the message...but all it did was flash..i didnt see the message :(
is that meant to hpn?

That's because DevC opens a window to run the program, runs it, and when the program ends, closes the window. Change your code to this:

#include <stdio.h>

int main()
{
    printf("This is output from my first program!\n");
    getchar();
    return 0;
}

Please use Code Tags, not colors. And please read this about using computer-speak.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Space Invaders, Pacman, and Pong! The holy three!

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

For all those people who are still brooding over the question, here's the solution ...

Well, a solution... ;)

Any credits for it doesn't go to me though; but to my faculty who had asked the original question..

My ghod, what a mess! If this is the formatting you are being taught, run fast! No instructor should ever give unformatted code to anyone, especially his students!

Best use of pointer i would say..

What say u??

If you like gobbledy-gook. I will assume it works, but even with formatting there is no way I'm even going to try to follow this code. Too much obfuscation just to prove it can be done. This is a perfect example IMO of "just because it can be done doesn't mean it should be done."

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Is there possibly a happy medium?

Yes, and I explained that to may4life. He seemed to understand, then ignored me.

As JoeProgrammer suggests

So if they're only stuck on one idea or concept, why not help them specifically with that part, instead of showing them the entire program? If they are capable of writing *most* of the program, let them do that. Let them get experience with that part. If they are truly stuck on a part, explain to them how to do it AND give them a code example. That's the best way of teaching someone.

Key word -- example! I go as far as to say that example does not even have to solve their problem, but shows a technique that they can use to solve it themselves.

may4life's argument is if you give them a working solution, the people that really want to learn will. I completely agree. It's the other 50-60% of them I don't want passing on someone else's work. There are enough hacks in the industry already. We don't need more graduates that barely know what they are doing because rather than figure it out, someone else does their work for them. It's these people that should not be given code. And if you really want to learn, they don't want it written for them. Therefore, the common denominator -- don't give working solutions. Give examples that explain the idea and let them solve it themselves.

I am a "newbie" - …

John A commented: Ahh... couldn't have said it better. -joeprogrammer +3
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You cant as such check for such conditions in case of a simple sort like bubble sort.

Take for eg. this array.
1 2 3 4 5 6 8 7

During the first seven passes no swap is performed since the next number is always greater than the current but a final swap is done in the last pass to sort the array. You cant as such terminate a bubble sort based on the flag that swap is performed or not.

Yes you can. You set the swap flag to FALSE between the two loops (inside the outer loop, before the inner loop). When the inner loop exits and a swap has been made, the swap flag should be TRUE. If no swap has been made, the flag should be FALSE and it's safe to exit.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

My personal preference:
All the colors should be of a similar intensity. The switch from the keyword-yellow to function-underline/blue is too striking.

To follow more closely what I've seen:
comments in green
Numbers, "Strings" and 'characters' in royal blue
Keywords in bold purple

All else is black
C/C++ functions don't need to be colored, but if they are, use a dark color like maybe red

[b]int[/b]  getCharacter()
{
    [b]int[/b] ch;             // define the character
    [b]do[/b]                  // loop until a good character is read
    {
        ch = getchar(); // read a character
    } [b]while[/b] ( (ch == 0x20) ||    // check for SPACE
             ((ch >= 0x09) && (ch <= 0x0D)) // check for the other whitespace
            );
    [b]return[/b] ch;
}
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

So my questions would be:

1. is my general idea alright?

Yes. But you should pass in the hex array. C cannot return a character array. Also, format your code. Indentation is extremely important at all times. So is spacing between terms, like:

while (fscanf(a, "%d", &broj) != EOF)

Much easier to read.

char dek_hex(int m){    /* this is where my problems start. No matter
                           how I write the function, I keep getting all 
                           kinds of error messages */

Unless you post something you've tried, we have nothing to help with.

2. what should my function look like?

Basic idea in my previous post.

3. can it be done without a function?

Yes it can. Considerations:
function: Modularizes the program, easier to reuse the function in another program later. Somewhat easier to debug.
non-function: Not passing the hex array is a minor benefit.

like I mentioned in my previous post, printf() family of functions, which includes ssprintf(), will convert int to hex using "%X" .

Wouldn't this nullify the purpose of understanding how decimal to hex works? ;)

~s.o.s~ commented: Well said Mr. WaltP -- ~s.o.s~ +7
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

One more suggestion -- format your code properly and wayward braces will no longer be a problem. Don't indent 3 tabs then next line 1 tab, then 2 tabs.

Your first brace should be far left and every line after indented 4 spaces. Each and every { indent 4 more spaces sterting with the next line. When you get to a } unindent 4 spaces, then add the } on a line by itself.

Then your code is easier to read:

void Look::printShipPos()
{
    if (checkFlag > 0) 
    {
        static int shipPos = 0;

        cout << shipPos << "_shipPos 1"<<endl;
        shipPos = shipPos + 228;
        cout << shipPos << "_shipPos added"<< endl;
    }


    if (shipPos == 228) 
    {
        std::cout << "French Polynesia [Out of Transmission Range]"<<endl;
        else if (shipPos == 456){
        std::cout << "Maui, Hawaii [Out of Transmission Range]"<< endl;
    }

    //---------------------counter for fly-over access
    checkFlag++;
    cout <<checkFlag<< endl;

    return;    
      
}
mattyd commented: TY :) +1
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

OK, it's time to back up and (sort of) start over. I haven't gone thru all your code, but you seem to be getting lost trying to solve 5 things at once. You never get anywhere that way.

Keep what you have because you can probably use most of it. Let's just rearrange stuff and do one step at a time.

#1:
Start with a new project.
Write the input portion of the code.
Input a string.
Output the string
When the string input and output are correct, move on, but not before!

#2:
Add the test whether the last character of the string is correct.
If not, do what you think you need to do.
Look at what you already have and use what you can.
When you can test the sentence correctly, move on, but not until!

#3:
Copy the input into another string character by character.
Copy only the characters you want to test as a palindrome.
Do not copy characters that aren't palindromish (space, comma, etc)
If you use string you should be OK. If you used char be sure to add that ending '\0'
Print out what you copied. When this looks correct, move on, but not before!

#4:
Add the ability during the copy to convert all uppercase characters to lower case.
Print out what you copied. When this looks correct, move on, …

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Hello...I need to write a program where i need to copy each word of a string into an array...
Eg(string s="hello how are you" arr[0]=hello,arr[1]=how....).
any help:?: ??

Thanks mate...but i need the solution in c++...is it posible??...sory for not specifying this at the start

In other words, "Thanks, but I can't turn that in as homework. Please write my homework for me properly."
:confused:

Grunt commented: Exactly :) [Grunt] +2
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

What's tha maximum value a signed char can handle?

Niklas commented: Thanks for your help. Didn't catch that. +2
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Arrays must have a name.

Not in a prototype. Onlyt the type of the parameter is needed.

Curtis:
The file must be opened in the execution portion of the code since opening a file is an action. You have it in the declaration portion of the code. Put it in main() Use code tags. It's much faster and easier than coloring your code by hand and preserves the structure -- assuming you have structure. ;)

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

As far as part c is concerned i will give you small snippet and you can do the rest of the parts yourself.

#include <iostream>
using namespace std ;

int main (void)
{  
   int g [5], counter;
   for (counter = 0; counter < 5; ++counter)
      g[counter] = 8 ;

   cin.get () ;
   return 0 ;
}

HOpe it helped, bye.

I don't read c that way.

c) Initialize each of the 5 elements of single-subscripted integer array g to 8.

To me that's a single statement. Define c and load values during the definition -- similar to the way array was handled.

I love instructions that are vague and interpretable... :confused:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Is it better now. sorry I did it in a hurry.

#include <iostream>
using namespace std;
int main()
{
     int number=10;
     int x;
     int your_number;;
     char answer = 'y';
     cout<< "\n\nguess the number:\n";
     cin >> your_number;
 
     while (your_number != number || answer == 'y')
     {
       if(number == your_number)
       {
             cout<<"Congrat!\n\n";
             cout<<"\n Want to try again? ";
             cin>>answer;
             if(answer == 'y')
                 your_number = 0;
             else
             break;
       }
       else
       {
             cout<< "\n Guess number:\n";
             cin >> your_number;
       }
    }
 
return 0;
 
}
 
this is not part of your code.  ending code tag should go here 
SAMPLE OUTPUT:
NOTE:It was run on a linux machine.
 
smg@samoguz-desktop ~/Desktop
$ ./num
 
guess the number:
2
Guess number:
3
Guess number:
10
Congrat!
 
Want to try again? y
Guess number:
6
Guess number:
7
Guess number:
10
Congrat!
 
Want to try again? n
smg@samoguz-desktop ~/Desktop

#1) Does while (your_number != number || answer == 'y') do what you want? Make a truth table.

#2) If your while loop is already testing if the number is == or != just let the loop end. Then verify outside the loop it's because of the == and output the results. That will make your while loop simple and easier to debug. Just put in it the stuff that has to go in it. IOW separate the 'work' with the 'result display'

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Try rebuilding the other apps. If they don't work it sounds like your libNcdd.so got corrupted and you need to replace it.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I haven't done this since I left DOS, but I believe you have to load the driver in the System information from the Control Panel. I don't believe config.sys is read in today's systems.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

What is the top decimal value that kbhit() catches when you hit a key? I didn't know about kbhit. So I coded the entire thing from scratch. It works. But the UP arrow key for example is " H", that is: NULL, capital h. It is actually 2 characters. Does kbhit() catch 2 characters? And could you provide a sample program that prints on the screen that an arrow key was hit, like I coded my sample program? I am curious about this apparent quick way that you provided.

kbhit() doesn't catch characters. It returns TRUE if a key has been pressed, FALSE if no key is waiting. That way your program can do whatever processing it needs and not wait for a key. In effect it makes the keyboard interrupt driven. Leave out the kbhit() call if you don't need to test for keyboard readyness.

As for a sample program, just output the value of the int egers ch1 and ch2.

Salem commented: rep++ Salem +3
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I usually do it with this:

if (kbhit())
{
    ch1 = getch();
    if (ch1 != '\0')
    {
            // process a normal keystroke
    }
    else
    {
        ch2 = getch();    // get the arrow or function key
        switch(ch2)
        {
                // process the arrow or function key
        }
    }
}

You can use this to discover what the key values are.

This only works for compilers that have getch() and kbhit() defined, of course.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

My bad. Let me know if you can read it now.

OK, now indent your code so it can be read. After every { indent 3-4 spaces, before every } unindent. Can't tell which open brace goes with which close brace with unformatted code.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

What was wrong with your first technique? It looked fine to me.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Well I coded for the backspace. Here is the code if anybody is interested...

if (str[num] == '\b')
      {

        str[num] = '\0';
        printf(" \b");
        str[num-1] = '\0';
        num = num - 1;
      }

I put it in a Do-While loop

A slightly better way may be:

if (str[num] == '\b')
{
    printf(" \b");
    str[--num] = '\0';    // sub 1 from num and set to \0
}

No need to zero the \b. Just zero the previous character.
Also, what happens if there are no characters and num is 0?

str[num] = str[num] - 'A' + 'a';  // converts it to lowercase

To convert to lower case, look up the tolower() function.

And as Ancient Dragon mentioned, what happens when num is 20 or greater?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Its normally a rule of the thumb to close the file pointer as soon as you are finished with it to avoid the undefined behaviour of the file stream which you have opened.
----
It is not called not writing into the files, as mentioned by me earlier, it is called undefined behaviour when the file stream is kept open.

Undefined behaviour can imply successful write as well as an unsuccessful write, but cant be determined when that happens. Use the funtion int ferror (FILE* file_ptr) to check whether the file stream is corrupted or not.

I'm not sure what you are saying here. It looks like you are saying if a file is not closed you enter the realm of undefined behavior. This is the first I've heard this. Can you point out something that substantiates this view?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You can't in Standard C. Some compilers have special functions that are not portable that can accomplisht what you want, but it's generally different for each compiler. So, if you want to enter the realm of non-standardization, we need more info.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I don't think I understand the problem.

I am a newbie to C++ and I'm not sure of anything at this point in time however,I got a problem with the starting phase of this program that well prompt the user to input a single character and output a inter4national Civil Aviation Word !!!!!

So you prompt the user to enter a character.

The prolem that I have is that you have to enter a character before it will start...

Ummm, isn't this the definition of "prompt the user to input a single character"? Why would this be a problem?

... and it closes after just one entry

Bottom of the program:

case 'Z': cout << "Zulu";
            break;
  default : cout << "Error";
}

cin >> a;

    return 0;
}

Output the information, accept a character (which reads the [enter] from above -- remember, you entered a letter AND the ENTER key) and exit. The only problem I see is the program probably doesn't wait at the end. Change the top cin to getline to keep the input stream clean, read it into a string and move the first character of the string to letter and continue.

Also, #include "stdafx.h" i not necessary.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Yes i completely agree with Mr. WaltP

Mr.? Makes me feel old.... :)

For small exercies it doesnt make a difference...

I really wish people would stop saying crap like this. It does make a difference whether large or small. It's just wrong. Period.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

In case you ever intend to compile that program as C++, you can't call main() in a C++ program.

And you should never call main() in C either.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
While (b not 0)
{
    test odd or even
    {
        save the result (0 or 1) in an array
    }
    shift b right 1 bit (or divide by 2)

}
output the array from last value to the first.

If you use the division version, b should be an unsigned value.

Alternate for the loop, if your value is 32 bits, simply use a for loop and you won't have to worry about signed/unsigned and your output would be a full 32 bits.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

What are something.X files and how to edit them?

Thanks!

Don't know. And if you don't know, why would you want to edit them?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

What have you tried? The code posted doesn't have any file read at all.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Now I wonder if someone can give an idea on how would be the best way to assemble a function to do this task. Maybe which C function would be the most appropiate to handle the strings and do the word extraction without touching the tags.

So you want to know which one single function you should use? That's a tough one :eek:

Seriously, the way to handle this is to
- read a line at a time
- remove all the html tags leaving only the 'real information' from the line
- then process what's left to get the information you want

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

YEs, there are quitre a few people here that can help. What have you got so far?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Multiplication generally works. Even for AU dollars.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

What S.O.S. said.

And indent more. 3-4 spaces, not 1, is standard.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

But it still doesn't work for me.
I'm just a freshmen. I set up a blank workplace, then win32 application project, then a c++ cource file.
Then type as below:

#include <iostream>

void main()
{
std::cout <<"welcome" <<std::endl;
}

No matter I use "main" or "WinMain" in the second line, it just doesn't work.
Also the linking problem.

This is not a "win32 application", it's a "console application". They are two completely different things.

And "it just doesn't work" tells us nothing. It outputs the Gettysburg Address instead of "welcome"? It turns your computer off? It has compiler errors? There are hundreds of ways it might not work. Be specific. Like void main() is wrong. It is supposed to be int main() with a return 0 at the end of the program, as defined by the C++ Standard.

And code tags are to surround all code postings. Begin code with [[I]code[/I]] and end with [[I]/code[/I]]

Salem commented: Agrees - Salem +2
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Write down the steps your program takes -- from top to bottom -- including inputs and variable values. Skip nothing.

I repeat -- write the steps -- this does not mean post code. Line by line convert your code to text, executing the statements as you go on paper.
When you have an input statement, what value on paper goes into the the variable. When you get to the while, what happens? When you get to the ifs, what happens?

This is desk checking your code -- a required step in most programs. I remember testing my code this way with little pieces of paper all around me while I sat on the floor. My boss thought I was nuts, but the program worked when I ran it...

And what is that dang getch() doing in there? I thought we got rid of that 5 hours ago! This is not helping me help you... Neither is your formatting. Indent 4 spaces after every { and unindent 4 spaces before every }. Each and every one!

~s.o.s~ commented: so helpful and patient, hats off to you Sir - ~s.o.s~ +2
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I thought this was supposed to start my loop and end it, if the user pressed 0. That was the intent....If the user presses 0, then the loop is supposed to end.....

What loop? [edit]rephrase -- Why loop? Why where you put it?[/edit]

Write down the steps your program takes -- from top to bottom -- including inputs and variable values. Skip nothing.

What do you come up with? Post it here...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Okay for your first question I should change the for statement to i<6

Yes now that you have pointed it out I see that there is a correlation between the two sets.

My outer loop is:
0, 1,2,3,4,5
the difference between that and what I need to print is
2,3,4,5,6,7
so the inner loop actually would appread to be the outer loop number +1 right?
so I could add 1 to i for the inner loop such as j=i+2; ?

Thank you

In both cases -- BINGO! Two loops is all you need. One outer for the lines, one inner for the *s based on teh line you're on...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

WaltP it never occured to me at the time to preview my post but your absolutely correct and thank you for the pointer on initialization of the variable.

Hmmmm.... no comment :)

My thinking was that I since I needed to print six lines of star's that I would have the outer loop run six times, ...

Good thought.

then I needed an inner loop to run six times but increment by 2 such that I would have another loop run 2 times, then 4, then 6, then 8, 10 and finally 12 times so that my cout <<STAR; statement would be printed by the same number each time through so that I would have the following output:

** print 2 times
**** print 4 times
****** print 6 times
******** print 8 times
********** print 10 times
************ print 12 times

You just complexed it out of reason.

Let's think about this... (yeah, I know that's what we're doing... ;))
You have one outer loop that goes from 0 to 6 for(i=0; i<7; i++) That's 7 lines... What's wrong here? That you can fix easily.

Now you want 6 lines of 2, 4, 6, 8... *s. Your loop goes 0, 1, 2, 3...
Is there a correlation between what you want and your loop?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

As I mentioned in another thread, "formatting is of primary importance. Every time you use { indent 4 spaces. Just before you use every }, unindent."

No I have never heard of that function. OK, I looked up fgets() and it says it is used to read a line of data from an external source, but I am not sure what that means. Here is the code I have so far...

Bummer. OK, change your getchar() to:

while ((myChar = getchar()) != '\n');  // clean the input buffer
getchar();
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

If I understand right I needed to enclose my code in [] right?

No. You enclose the code in tags. You could possiblly use the PREVIEW button to see if your post looks right... ;)

I re-wrote the for statements although I thought that it was okay to declare variables earlier in the program and just place a ";" in the place of the the variable declaration in the for statement? I was unable to get the k variable to compile without first declaring it.

Yes you should declare your variables earlier in the program. And yes you can just ";" if you know what you're doing. The first position is not a variable declaraction is a variable initialization, the first value the variable has thru the loop.

Also, formatting is of primary importance. Every time you use { indent 4 spaces. Just before you use every }, unindent.

Formatted properly, you code looks like:

#include <iostream>
#include <string>
using std::cin;
using std::string;
using std::cout;
using std::endl;
int main()
{
    int i;
    int j;
    int k;

    const char STAR ='*';
    k=2;                    // What does this statement do for you?
                            // What's the value the second time thru the I loop?
    for(i=0; i<7; i++)
    {
        for(j=0;j<k;j=j++)
        {
            cout << STAR;
        }
        {                   // Why are you starting a new block?
            for(k=0;k < 14; k= k+2)
            cout << endl;
        }
    }
    return 0;
} //end of main function
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I take it back.... you used scanf() -- therein lies your problem. It leaves your input buffer dirty. Do you know the fgets() function?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Don't know how you changed your program. What's it look like from the while down?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Where are the code tags?
Where is main() defined (unresolved external symbol _main)
Did you link with the debug library?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Format your code properly with code tags and finish your for statements (3 parameters, not 2) and repost...

Then we have something readable to work with.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Why not try to make the variable persistent throughout the program by declaring it as global static variable ?

You can best (I think) make an global variable or an automatic variable in main called counter and then add it if the button is pressed.

No, you shouldn't make it global if you don't need it outside the function.
Don't know what you mean by an "automatic variable in main called counter"

i think u better to use static counter instead of counter so that it can retreive the value in betwwen the function calls

Yes. Simply define the counter as static int counter; instead.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Hey this problem is too simple to implemented in an objecft oriented way. Programs this small do not need to have good programming practices. Especially if teyt are just for fun. For somthing this small it only matters if it works IMO.

Well said, if all you want to be is a hobby programmer. For anyone looking at programming as a profession, this is the worst advice they can receive.

All programs should be written using good programming practices. It never "only matters if it works." Have pride in your work.

Im not that good a coder and this program is unweildy and long...

Now we know why... ;)

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

probably because BGI requires a 32-bit compiler, which TurboC is not. Upgrade to one of the free compilers mentioned in that link.

Just the opposite. I was using BGI back with Turbo C++ 1.

Download from Borland:
Turbo C++ version 1.01
Turbo C++ version 2.01

Other old Borland stuff can be found here.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

the problem is how could i store the current record in the variable ? Can you give some example ?

I have tried it before but the previous input have been overwrited...

Did you read in the current record? Is it still in the variable space? If so, move it to a new variable (maybe called LastRec) just before you go back to read the next record from the user.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Even I am not sure what he meant but I didn't ask because that would have spoiled his fun :D

See Wolfpack's signature...