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

To debug your code, add output statements in key places to show you what key values are at each step of the program. If one of the values displayed is wrong, you now look at the code to find out why. Of course, you need to know what the values should be at every step.

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

Look at each character. When you see an H remember it. If the next letter is E, remember it, else forget everything. Continue until you have all the letters. Now check if there is a letter before or after these letters. If so, ignore it all, otherwise, change the characters

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

Read a character at a time. If it's a letter, add it to the matrix. If it's a \n, move to the next row in the matrix.

Read the words into an array.

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

OK, you need to think about what you need. getchar() inputs a single character. putchar() outputs a single character.

When you input a character,
1) if it's a letter, output it.
2) if it's not a letter,
2a) if it's the first non-letter, output a \n
2b) if it's not the first non-letter, output nothing

You need to keep a counter for the non-letter characters. I leave it to you to figure out how to do it.

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

You have to create the "file1.dat" filename each time through your loop. You need stringstream or sprintf or something similar to do that.

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

You aren't thinking clearly. Here is your code:

for(int x=0; x<strl1+1; x++){     // Why are you going forwards? Won't this add 
                                      //    the carry to the wrong value?
        sum=num2[x]+num1[x]+carry;    // Where did you initialize carry? It could 
                                      //    be 7023479 because it's uninitialized
        if(sum>9){
        carry=1;        // True
            }
        else{
             carry=0;        // True
             }
        sum=sum-carry;        // If carry is 0, why? Shouldn't this go
                              //    where sum > 9?
        total[x]=sum;
        cout<<total[x];
        }
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

line 35 of the first block of code.. as i said earlier, the second if statement of my populate random function. please read what I said in the original post first.

I read it... You said

I tried debugging it, and found that there is something wrong in the second if statement in my lifePopulateRandom function.

Then you did something wrong somewhere. Maybe a more accurate description of what something means is required to understand your problem.

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

the parameters taking of printf function is from right to left.....
So in the given problem the function executes as follows....

b=1;
printf("\n %d %d %d",b=15, b>9, b<9);

So first it executes the condition b<9 but here b=1
next it executes the condition b>9. Here also b=1
and the last step is b=15 will executes.........

Please reread Narue's post. She is correct, you are mistaken.

i get this but why it happens only in case of printf???

What makes you think it only happens with printf?

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

Let's put this in perspective. I will rewrite your question and see if you think your request is appropriate.

Hello, I am new to being a doctor (3rd year student) and I would like to perform my first surgery something similar to open heart surgery obstacle is how to find a patient willing to let me do the surgery, I know how to hold a scalpel and I know where the heart is, but how do I get through the rib cage to get at the heart??

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

Use cin.get() or cin.getline(). Both are C++ input, rather than non-standard C input.

And get rid each and every goto statement. They are not needed if you program your case statements correctly and add appropriate loops your program.

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

I would like to make a program which allows you to enter a number (say 145). It reads the 3 CHAR and prints the largest one.

I have used for loops but not arrays!! So it would be great if I could do it in Arrays..

Input the number as CHARs like you already mentioned.
Use a loop on the array (sequential collection of CHARs):

char num[10] = {0};     // create the array, all defined as 0
char largest= 0;        // create the 'largest' value -- as 0

cin >> num;             // reads in all the keystrokes into the array
for (i=0; i<10; i++)
{
    if (largest < num[i]) 
    {
        largest = num[i];  // replace with the larger value
    }
}
cout << largest;
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

And do you think that this is the easiest example?????

Who cares if it easiest? It's damm easy so highly understandable and gets the point across -- more so than an example that shows efficiency too.

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

What open source software for PDAs/smart phones (like Blackberry )is best for student attendance monitoring/tracking? Any specific/brand of software? Or any possible specific hardware for that software?

If there was a best there wouldn't be choices...

peter_budo commented: Bullseye, but that doesn't mean you are the best ;) +0
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Are you required to use arrays, or can you use vectors? Using a std::vector will make your life much easier because a vector is easy to change and re-size (dynamic) whereas arrays are not readily adjustable (static).

If he doesn't know classes it's a sure bet vectors are out of the question...

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

What I don't understand is that why the book gives an example which does not really show the efficiency of the switch-statement. This example could easily be done and easier to understand using if-else statement...

Because when you are learning, the easiest example is the easiest to understand. Doh!

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

As long as I have C++ installed on my workstation, it will not die ..

Ahh, then BASIC is safe, too. I have it installed on my computer...

Sky Diploma commented: Hehe :) Me too ! +0
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You don't. You read the first line, then the second, then the third...

I thought you were reading a map. Why would you want only one line of the map?

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

It's the web link to the post itself. You could have just clicked it...

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

You are doing it the hard way.
First, make the dimensions of grid large. This way you can have different size maps if you want them.

Read a line into the first row of grid[0]
Read the next line into grid[1]
etc.
Count the number of lines as you read them.

Then just output each row in a loop.

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

I guess you may also need a lamp and cant see a toileteries for grooming oneself after using the toilet

We're programmers. We work from the light from the screen. And since we got no girls (being nerds), we don't need no groomin' neither.

ProgrammingGeek commented: Haha +0
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You can use IF statements, SWITCH statements, ARRAYs, and FUNCTIONS to make your code more compact. You don't need 37 IF statements to test the number field...

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

I have an input file that i make into a string, it looks like this

"1+2+3+45*5*2"

My problem is when i print the string it always prints as single characters
instead of 45 i get 4 5, how do i fix this. heres a small piece of the code.

cout<<string; //string content 1+2+3+45*5*2
int size=string.length();
cout<<string[7]<<endl;
cout<<string[8]<<endl;

output is always 4
5

Of course it is. A character of the string is 4. The next character of the string is 5. There is no character '45' -- that's two characters.

You have to separate the 'values' from the 'operators' and deal with multiple digits yourself.

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

I'd do something a little more 'noobie' (starting from Narue's code):

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

int main()
{
    int  n;
    int  val;
    int  array[MaxSize];
    bool done;

    for (n=0; n<MaxSize; n++) array[n] = 0;
    srand((unsigned)time(nullptr));
    
    cout << "Enter N: ";
    if (cin >> n) 
    {
        done = false;
        while (done)
        {
            array[rand() % n]++;
            for (val = 0; val < n; val++)
            { 
                if (array[val] == 0) break;
            }
            if (val >= n) done = true;
        }
        // display what you want...
    }
}

At the end, array[] will contain the number of times each random value was generated. Add them up and you get the number of time rand() was called.

Keep in mind the technique of calling rand() until all numbers are generated could never end. There's no guarantee that all numbers will be generated -- as per definition of random.

My reason for using the noob technique is most early students have no idea what a vector is nor the algorithm package.

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

Ahh, OK.

By the way, see this and this

sergent commented: Time for me to start learning Cobol +0
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Cobol runs on Windows 7, Linux, and the current leading edge IBM Mainframes. So why are you implying only ancient systems? It is still being used and developed in many companies -- not just being maintained because they can't rewrite the code.

Cobol is still the spine and nervous system of many companies -- Fortune 500 included.

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

Yeah! I was going to mention the fact that COBOL is the worst language in the world and yet most internal software depends on it. It is one of those old, spaghetti-code languages that has about a billion person hours invested and limps along because -er- no one knows how it does what it does using computed goto statements for gawds sake.

Sigh

It's very obvious you have never studied COBOL with this asinine synopsis of the language. For what you want to do, it may be a bad language. But that is far from the truth. The reason it's still around is because it is a very good language for what it does and the arena it's used in. Believe me, if it was bad for the reasons you stated, it would have been replaced long ago. The fact that it's hasn't been shows it's usefulness.

Even the (IMO) gawd-awful RPG language, it does what it's supposed to. Effectively.

MooGeek commented: Soo true +0
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Hmm, without knowing anything at all about the OP's original purpose, this thread has become a hate-war over clearing the screen.

Yes, and it was thankfully dead for 6 years until a wasted person resurrected it with a worthless suggestion.

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

And I'm still waiting for you to

1) Place comments on each line of your code that explains why that line is there. "Because someone said so" is not acceptable.
2) Write down the steps needed in detail to check the beginning of the file for your bad file indicators
3) Start outputting values to see if each variable, each array element, etc contains what you think it should -- based on #2

I didn't suggest this to be mean. I suggested this so you can learn something. It will help.

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

fdeposit stores the balance after a deposit, fwithdraw stores the balance after a withdrawal, and balance is the initial balance. The initial balance is $500.00.

I had defined the variable total to keep the running total but I did not know how to include it with the static number.

Why? What is the purpose of 4 different balances? Do you really care about your balances after each deposit and withdrawal as separate values? Or is a deposit or withdraw simple a value added/subtracted from the overall balance?

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

That's what I can't figure out, suggestions would be appreciated.

Well, if you start by testing if num == die , you now know if the guess is correct or not right away. Why do you need to test for more?

Any comments on my other points?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
int num;
    int die;


    cout << "What number do you think you'll roll? " << endl;
    cin >> num;
    
    die=(int)(rand()%6)+1;              // So far, so good
    
    if (num<6 && num>0) {               // So 0 is a proper guess? But 6 is not?
            srand((unsigned)time(0));   // Why are you seeding the random generator
                                        //    after you got your random value?
            cout << die << endl;        // And what good is displaying the die in this case?
    }

    else if (num==die) {                // How can you get here if the above IF is true?
        cout << "Congradulations! You predicted correctly. " << endl;
    }
    
    else if (num > 6) { 
        cout << "Number not on die" << endl;
    }
                            
    system("PAUSE");            // Not a good way to pause the program
    return(0);

srand() needs to be called once at the start of the program.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
case 1:
		fdeposit = depositBalance (deposit);
	cout << "After your deposit your current balance is $" << fdeposit << endl;
		continue;

	case 2:
		fwithdraw = withdrawalBalance (withdrawal);
	cout << "After your withdrawal your current balance is $" << fwithdraw << endl;
		continue;

	case 3:
		cout << "Your balance is $" << balance <<endl;
		continue;

What is the purpose of the variables fdeposit, fwithdraw, and balance?

I cannot think of how to set an accumulator for the transactions or how to keep a running total of the balance after each withdrawal and deposit.

What is the variable you defined to keep the running total?

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

Yes, start over. 260 lines is way too many for this program.
1) Get rid of ALL the system("cls") calls -- they are annoying to the user at best.
2) NEVER use a goto statement. They are not needed in 99.99% of C/C++ programs.
3) In each switch statement for the cpuPick add another switch statement for the playerPick and set a simple result of win, loss, tie. Only.
4) After the switch statements, print the results.

Should be a very short program.

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

I'm having trouble getting this program to work for roman numeral conversion. I hope it says that i have one error, which is C2110: cannot add two pointers. here's what i have so far... ANY help would be great

1) Use CODE Tags
2) Don't tell us you hope for an error, tell us exactly what error you get
3) Don't make us guess what line the error is on. Tell us. We aren't psychicprogrammers.com you know.

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

Look, you can't program by
1) running the code,
2) seeing that it doesn't work
3) arbitrarily changing something
4) going back to 1

You need to
A) Think about what you are doing
B) Test what you try
C) If it doesn't work, find out why
D) Fix it

I want you to do three things:
1) Place comments on each line of your code that explains why that line is there. "Because someone said so" is not acceptable.
2) Write down the steps needed in detail to check the beginning of the file for your bad file indicators
3) Start outputting values to see if each variable, each array element, etc contains what you think it should -- based on #2

And if your present code doesn't look like it will be easy to fix, start over and do only the beginning-file test. Once that works, add the end-file test.

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

You said the code isn't designed to work, were you referring to the code i originally posted? Or to sundips code?

Sundips. Didn't you bother reading my post between his code and your attempt? I explained exactly what was wrong with his code.

The maximize size the file can be is 60kb. And no more than 20 lines.

To test the 1st character, read the first line. Test for and ignore all initial SPACES. If the first non-SPACE is a digit, you're OK, otherwise a bad file.
To test the last character, read the rest of the file. When you get to EOF, test the last characters in your buffer for whitespace (which includes the \n) which you skip. The first non-whitespace character also needs to be a digit, otherwise the file is bad.

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

How much programming do you know?

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

So change all the printf() 's

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

Can anyone tell me what I did wrong?

Using code that obviously isn't designed to work, as I stated already.

You are making this much more complicated than it has to be.

How large do you expect the file to be? (maximum)
How many lines are you expecting to have? (maximum)

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
....
        buffer[BUFFER_SIZE] = '\0';        /* make sure that the string gets delimited */
...

Useless. By definition fgets() adds the '\0' for you. And it will go into buffer[BUFFER_SIZE-1] if you fill up the entire buffer.

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

Then hide the quick-reply on threads that are older than N days.

And from the standpoint of "significant number", as far as mods are concerned, yes.
And from the standpoint of "other forums", who cares. We are not "other forums". We are allowed to be better than them.

I worked with a warehouse storage system which guaranteed no more than 1% misstored (lost) merchandise. Sounds insignificant. When you calculated that 1% it came out to hundreds of pallets of merchandise. No longer insignificant.

mitrmkar commented: Very good point on"other forums". +0
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Start by adding print statements to be sure you are reading the input properly.

Program one step at a time.
First input. Make sure it works.
Next internal representation of the values -- print them. Make sure they are correct.
Next...

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

change printf() to fprintf()

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
char firstChar[256];
size_t chars_read= 0; 
int currentLocation = 0;

while (input.good())
{
  nextChar = (char) input.get();
  firstChar[chars_read++]=(char)input.get();
}

So now in the firstChar[] array you have every other (even numbered) characters from the file. 1st, 3rd, 5th ... characters are not in the array. And this is supposed to help how?

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

So pretty much filestream for the file, and read it so if it says:

Name:Rat
Attack: 10
Defense: 10
Health: 20

I would get it to read each line specifically , and take the value after the colon? And store it under a string? Or what? If not a string

It's your file. Define it any way you want.
Is "10" useful to you as a string?

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

this is not my homework..but its just my exercise..i have done to built this..but it cant run properly..

Why not? We know less about your program than you do.

can somebody help me and explain what the question want..i dont understand the question..

Seems obvious to me. What you don't understand?

Do not give vague statements and expect us to understand what's in your mind. You need to explain and ask good, detailed questions.

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

I'm addicted to Mountain dew but I stopped drinking coz I was afraid I'll get a diabetes :(

Watch out for the withdrawal symptoms -- an uncontrollable urge to pick up a banjo and play Foggy Mountain Breakdown by the cee-ment pond.

ProgrammingGeek commented: Haha! +0
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

What ^he^ said...

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

Create a data file where each line defines the values for each monster. Then create a read function that reads the file. This way you can add and modify monsters any time you want and won't have to change the program.

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

Water? Never!!! Mountain Dew -- the drink of techies.