daviddoria 334 Posting Virtuoso Featured Poster

1)I have to include

#include <cstdlib> // for srand

to define the srand() symbol.

2) There is no reason for all of this:

random_integer = rand() % (HIGH - LOW + 1) + LOW;

Since you just want a number from 0 to 1, you can just use rand() 3)Your main problem is that you are trying to compare 'h' or 't' to 0 or 1! This will not work!

Below is how I would do it. I just set a char coin variable based on the int variable that you generate. Then I compare the char guess with the char coin.

#include <iostream>
#include <ctime>
#include <cstdlib> // for srand
using namespace std;
int main ()
 {

     char guess;
     char ans;
     int no_of_plays;
     const int LOW = 0;  //It will auto range the output to within H and T
     const int HIGH = 1;
     int random_integer;

     cout << "THE GAME OF HEAD OR TAILS" << endl;
     cout << "" << endl;
     cout << "Do you want to play? " << endl;
     cout << "Y/y for yes, N/n for no " << endl;
     cin >> ans;

     if (ans == 'y' || ans == 'Y') {
        cout << "How many times do you want to play?  " << endl;
        cin >> no_of_plays;
     }
     else {
          cout << "Thanks for playing!  " << endl;
     }

     for ( int a = 0; a < no_of_plays; a++)
     {
        cout << "Call it, Heads or Tails?" << endl;
        cout << …
daviddoria 334 Posting Virtuoso Featured Poster

I think even though you have include guards, you can't declare a function in the header, you can only define it. That is, you need to change the header to:

#ifndef GENERALFUNCTIONS_H
#define GENERALFUNCTIONS_H

bool IsFiniteNumber(double x);
    
#endif /* GENERALFUNCTIONS_H */

And then in the implementation (.cpp file), put:

#include "GeneralFunctions.h"

bool IsFiniteNumber(double x) {
     // thanks to http://www.johndcook.com/IEEE_exceptions_in_cpp.html
        return (x <= DBL_MAX && x >= -DBL_MAX); 
}

David

daviddoria 334 Posting Virtuoso Featured Poster

Use a while loop:

cin >> ch;
while(ch != some_exit_value)
{
 // do you conversion
 // cin >> ch;
}
daviddoria 334 Posting Virtuoso Featured Poster

Your sarcasm is unwarranted. You have to "help us help you". You are not paying for this help, so if it is annoying, no one will help you (as you noticed).

daviddoria 334 Posting Virtuoso Featured Poster

What is says is that you cannot pass a variable length 2D array to a function. You have to know the dimensions ahead of time, and hardcode them in the function signature.

This works properly:

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

void updateGrid(char grid[][2]);

int height = 2, width = 2;

int main(int argc, char *argv[])
{
    // CREATES A GRID 2X2 AND POPULATES IT
    char grid[2][2];
    grid[0][0] = 'a';
    grid[0][1] = 'b';
    grid[1][0] = 'c';
    grid[1][1] = 'd';
    for(int i = 0; i < height; i++)
    {
            for(int j = 0; j<width; j++)
            {
                    cout << grid[i][j];
            }
            cout << endl;
    }

    // to run function type update
    string str;
    cout << "type update to run function:";
    cin >> str;
    if(str == "update")
    {
           cout << "\n\n\n";
           updateGrid(grid);
    }

    system("PAUSE");
    return EXIT_SUCCESS;
}

void updateGrid(char grid[][2])
{
     for(int i = 0; i < height; i++)
     {
         for(int j = 0; j < width; j++)
         {
            cout << grid[i][j];
         }
         cout << endl;
     }

}

If you cannot know the size of the grid at compile time, you must use dynamic memory - either in the form of std::vector's or use malloc/new to allocate the array.

char* x = (char *) malloc(num_rows * num_cols * sizeof(char));

I'm not sure if

char* x = new char[num_rows * num_cols];

does the same thing. I'm also not sure if you automatically get the [j] style accessors with the above methods.

daviddoria 334 Posting Virtuoso Featured Poster

Again, please use code tags.

You have read the value into 'u', and then overriden it by 'l-32'. Worse, 'l' is undefined.

Also, by the way, this is just operating on a single character.

daviddoria 334 Posting Virtuoso Featured Poster

Please use code tags when you post code.

That program has nothing to do with case changing!

You should check the input, one character at a time, and see if its ascii value falls in a specified range (65-91 for upper case, etc). If it does, modify it. Doe the same for the lower case characters.

You'll have to show us you've tried before getting much more help :)

David

daviddoria 334 Posting Virtuoso Featured Poster

Did you look at the link I sent? Like I mentioned, it had several solutions for you.

daviddoria 334 Posting Virtuoso Featured Poster

doolali, that is called a global variable. They are VERY BAD. They may seem like a nice idea when you only have a short program, but when the code gets bigger global variables make things very difficult to manage.

daviddoria 334 Posting Virtuoso Featured Poster

You cannot replace a line in a plain text file like this. You'd have to either
1) Read the whole file into memory, replace the line in memory, then rewrite the whole file or
2) Use an actual database (SQL or similar)

daviddoria 334 Posting Virtuoso Featured Poster

Your DUTY class does not have a getName() function, hence you have to first get the instance of a class (a FLATMATE) which does have a getName() function (WashDutyList.at(j)).getResident()) and then call getName() on it.

daviddoria 334 Posting Virtuoso Featured Poster

FYI the terminology is "passing a 2d array to a function".

Here are multiple solutions:

http://www.programmersheaven.com/mb/CandCPP/315619/315619/how-to-pass-a-two-dimensional-array-by-ref/

I would opt for the "use a vector of vectors rather than a 2D array" option.

David

daviddoria 334 Posting Virtuoso Featured Poster

This may be fancier than your instructor would like, but it is the "correct" (modern, c++ style) way to do it.

http://programmingexamples.net/index.php?title=CPP/Strings/Case_Conversion

Are you using std::string at all? Or character arrays?

David

daviddoria 334 Posting Virtuoso Featured Poster

Unfortunately I can't look into this enough to help you with the assignment.

pred is a struct object. vPred is a struct type of vector.

Of course, I read the code :). My point is that the word "pred" does not tell me what it is. Is it a "predator"? A "predominant something"? See what I mean? Then "vPred" could be called "predators" or something, though vPredator might be acceptable?

daviddoria 334 Posting Virtuoso Featured Poster

Here is my initial feedback:
1) You should make variable names and class names as descriptive as possible. What is a "Pred"? What is 'vPred'? What is 'x' and 'y'? Why are they strings?

2) (1) is also aided significantly by the heavy use of comments!

3) What is your goal? You may be better off using another container besides std::vector (std::set comes to mind) if you are trying to simply store unique elements).

Give us some more clues and we'll try to help further :)

David

daviddoria 334 Posting Virtuoso Featured Poster

I have a few comments/suggestions.

1) user_guess is unused. You should setup your compiler to tell you about this - mine gave me a warning when I compiled your code.

2) There is no reason to use defines here - it is bad "c++ style".
At the least, change the defines to global variables:

#define MAXGUESS 100
#define MINGUESS 1
int MAXGUESS = 100;
int MINGUESS = 1;

at best, there is no reason to use global variables here at all!

3) You have a 110 line function (main). This is pretty long. I would consider breaking out functions like

bool IsGuessCorrect(int guess, int correctNumber);
char GetUserResponse();

The main function would then look like:

do
{
char response = GetUserResponse();
} while(response != 'y' && !IsGuessCorrect(guess));

(clearly not exactly like that, but the point is that the structure of the program is very simple, and main should reflect that.)

Good work!

David

daviddoria 334 Posting Virtuoso Featured Poster

I would actually imagine they are assuming the computations take 0 time. So you just need to make a function wait(2) that pauses for 2 seconds.

daviddoria 334 Posting Virtuoso Featured Poster

Please use code tags. That is

[ code ]
your code
[ /code ]

(but remove the spaces between the brackets and the word "code")

daviddoria 334 Posting Virtuoso Featured Poster

I agree with Kontained.

To generate the numbers ("call" them in Bingo), I would make a vector of all possible numbers and then shuffle it:
http://programmingexamples.net/index.php?title=CPP/STL/RandomShuffle

daviddoria 334 Posting Virtuoso Featured Poster

If possible, please post a reasonable compilable length of code using code tags. You will likely get many more responses if people can look at your code directly in the browser versus having to download and open it.

daviddoria 334 Posting Virtuoso Featured Poster

I thought I had the idea, but it doesn't seem to be working:

<HTML>
<head>
<script>
function showWH(){
cW=document.body.offsetWidth
cH=document.body.offsetHeight
window.resizeTo(500,500)
barsW=500-document.body.offsetWidth
barsH=500-document.body.offsetHeight
wW=barsW+cW
wH=barsH+cH
window.resizeTo(wW,wH)
var embedEl = document.getElementById("embedId");
embedEl.setAttribute("HEIGHT",wH);
embedEl.setAttribute("WIDTH",wW);
}
</script>
</head>
<BODY>
<center>
<OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" CODEBASE="http://active.macromedia.com/flash5/cabs/swflash.cab#version=5,0,0,0">
<PARAM NAME=movie VALUE="ObtainingAndBuilding_Linux.swf">
<PARAM NAME=play VALUE=true>
<PARAM NAME=loop VALUE=false>
<PARAM NAME=quality VALUE=low>
<EMBED id="embedId" SRC="ObtainingAndBuilding_Linux.swf" quality=low loop=false TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">
</EMBED>
</OBJECT>
</center>
</BODY></HTML>

Any thoughts?

Thanks!

daviddoria 334 Posting Virtuoso Featured Poster

Ah, very cool. Thanks.

David

daviddoria 334 Posting Virtuoso Featured Poster

I have a line like this:

<EMBED SRC="ObtainingAndBuilding_Linux.swf" WIDTH=200 HEIGHT=300</EMBED>

that I want to replace 200 and 300 by the browser window width and height.

I found some code like this which correctly gets and displays the width and height:

<HTML>
<head>
<script>
function showWH(){
if (document.all){
cW=document.body.offsetWidth
cH=document.body.offsetHeight
window.resizeTo(500,500)
barsW=500-document.body.offsetWidth
barsH=500-document.body.offsetHeight
wW=barsW+cW
wH=barsH+cH
window.resizeTo(wW,wH)
}
else
{
wW=window.outerWidth
wH=window.outerHeight
}
alert(wW+' '+wH)
}
</script>
</head>

<BODY onload="showWH()">
<EMBED SRC="ObtainingAndBuilding_Linux.swf" WIDTH=200 HEIGHT=300 quality=low loop=false TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">
</EMBED>

</BODY></HTML>

However, when I replace the hard coded with and height with:

WIDTH=wW HEIGHT=wH

The object gets very small (i.e. it is not any where near the window size.) Is there some syntax I am missing to get the value of these variables? (like a "${wH}" or something?)

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

My trouble is with the first book title input. It prints out both booktitle and date published on the same line. It doesnt happen for the rest of the titles though.

For questions like this, I would attempt to narrow down the problem as far as possible. That is, rather than show us 100 lines, probably only 3 of which have anything to do with this, create a 10 line compilable example which demonstrates the problem.

secondly, i would like to implement a calendar class for the input of the dates. Can anyone guide me in the direction?

This is tricky. To do it well is very complicated. I'd strongly suggest using a library that already has such functionality. Boost is sometimes a bear to work with, but here is where you should start: http://www.boost.org/doc/libs/1_43_0/doc/html/date_time.html

If you get it to work (or anyone else knows how to use it), it would be great if you could add a reasonable example to the "Boost" section here:
http://programmingexamples.net/index.php?title=CPP#Boost

Good luck.

David

daviddoria 334 Posting Virtuoso Featured Poster

Here are some examples of simple operations using std::list :
http://programmingexamples.net/index.php?title=CPP/STL/List

As for making a student database - I'm assuming you just want to make a class Student and then store the students in a list?

daviddoria 334 Posting Virtuoso Featured Poster

I think descriptive variable names and comments would be helpful here (and everywhere... :) )

I'm assuming 'tall' is the height? If so, name it 'height' and change the name of the function to GetHeight() or something like that. What is 'tall1'?

Add a comment to at least every conditional and every loop and I bet it will be come clear to you what is going on/wrong.

David

daviddoria 334 Posting Virtuoso Featured Poster

After reading some tutorials it looks like this is how to embed a video with html5:

<html>

<body>

<video width="560" height="340" controls>
<source src="big_buck_bunny.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
</video>

</body>

</html>

(That video is in the same directory has the .html file)

When I do this, I see the control bar, but the video doesn't seem to load/play. Anyone know what could be wrong?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

Hi clueless215 - welcome to DaniWeb. Here are a few guidelines which will help you get the most out of the forum.

1) When you have a new question, start a new thread rather than post at the end of someone else. What you have done here is called "thread hijacking" and is frowned upon.

2) Use code tags. That is, surround your code with [ code ] your code [ /code ] (without the spaces inside the brackets. It makes it much easier to read.

3) Don't use abbreviations like 'hmwk'.

As to your question - you have done the hard part already! (Identifying that "%2 = 0" determines if the number is even.) Now you just have to make two sum variables (sumEven and sumOdd) and use them to store the count along the way (sumEven += number).

Good luck,

David

daviddoria 334 Posting Virtuoso Featured Poster

I'd suggest printing the sum at each iteration of the loop to see what is happening. If you can't get it, post code without any user input that produces a result that you are not expecting.

daviddoria 334 Posting Virtuoso Featured Poster

Use nested for loops.

daviddoria 334 Posting Virtuoso Featured Poster

Can you post a very short ( < 30 lines) piece of compilable code that demonstrates the problem?

daviddoria 334 Posting Virtuoso Featured Poster
daviddoria 334 Posting Virtuoso Featured Poster

There should be a file probably called msoftcon.cpp that you need to include in your project. These functions are not defined in the file you have posted, so you can't use them unless you link to the file that defines them.

daviddoria 334 Posting Virtuoso Featured Poster

What kind of tags did you use? You should use

[ code ] your code here [ /code ] (without the spaces between the word 'code' and the brackets. Also, if you remove the massive comments the code will be much more manageable for the forum :)

daviddoria 334 Posting Virtuoso Featured Poster

Here are many things you can do with std::vector :
http://programmingexamples.net/index.php?title=CPP/STL/Vector

daviddoria 334 Posting Virtuoso Featured Poster

Please use code tags.

daviddoria 334 Posting Virtuoso Featured Poster

Please use code tags. It really helps readability.

You'd have to have an instance of Robot in the Motores class in order to call that function.

daviddoria 334 Posting Virtuoso Featured Poster

I would try to simplify the problem down to < 50 compilable lines that you can post directly to daniweb (using code tags). It is much easier for people to review than having to download your files.

daviddoria 334 Posting Virtuoso Featured Poster

I'm not sure why you would need to use pointers here?

You should definitely use a loop though:

for(unsigned int i = 0; i < 10; i++)
{
amount[i] = price[i] * quantity[i];
}

If you use an std::vector, you can loop until amount.size() instead of hard coding "10" :)

David

daviddoria 334 Posting Virtuoso Featured Poster

You haven't shown us the code where the problems are occurring. It should be easy to spot a "too many arguments" type of problem. Have you included <stack> ?

That is the only reason I would say push() would not be defined. Here is an example of using stack:
http://programmingexamples.net/index.php?title=CPP/STL/Stack

David

daviddoria 334 Posting Virtuoso Featured Poster

I don't see why you'd need pointers here, but you definitely should use a loop:

for(unsigned int i = 0; i < amount.size(); i++) // note you can only use .size() if you use an std::vector, which you should definitely consider!
{
  amount[i] = price[i] * quantity[i];
}

David

daviddoria 334 Posting Virtuoso Featured Poster

I don't see why you'd need pointers here, but you definitely should use a loop:

for(unsigned int i = 0; i < amount.size(); i++) // note you can only use .size() if you use an std::vector, which you should definitely consider!
{
  amount[i] = price[i] * quantity[i];
}

David

daviddoria 334 Posting Virtuoso Featured Poster

@mcroneil - Please show us your attempt and we can help you debug it.

@rheg - You should start a new thread when you have a new question.

daviddoria 334 Posting Virtuoso Featured Poster

Which problem? The one where it doesn't know that all return paths actually do return a value? I'm not sure actually - I haven't seen it in a while and I can't reproduce it now, I've just seen it in the past so I was trying to prevent it. But all the suggestions here certainly help me do that - thanks all.

daviddoria 334 Posting Virtuoso Featured Poster

Sorry, I made the example too simple - maybe something more like:

int function(int a)
{
  if(a == 2)
  {
  // do something
  return true;
  }
  else
  {
  // do something else
  return false;
  }
}

I guess even this can be changed to what you suggested:

int function(int a)
{
  if(a == 2)
  {
  // do something
  return true;
  }

  // do something else
  return false;
}

Interesting... it doesn't look as clear to the reader at first glance (at least to me) but I suppose it fixes the problem... :)

daviddoria 334 Posting Virtuoso Featured Poster

In the past when I've done this:

int function(int a)
{
  if(a == 2)
  {
  return true;
  }
  else
  {
  return false;
  }
}

the compiler has complained that the function may not return a value. I've fixed this by adding:

int function(int a)
{
  if(a == 2)
  {
  return true;
  }
  else
  {
  return false;
  }

  return false;
}

but now a different compiler is complaining that the last line is "unreachable code" (which I agree with). How is this usually handled?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

Hi avaughn, welcome to DaniWeb!

If you look at the post by cscgal above, you will see all of the elements that you need. The "if number%2 == 0" test is how to tell if a number is even or odd. Then you just have to keep track of which ones are which! Please give it a try and post some code and we can help if you get stuck.

David

daviddoria 334 Posting Virtuoso Featured Poster

I see... why do they provide these make_heap functions then?

daviddoria 334 Posting Virtuoso Featured Poster

I just learned about heaps today. It looks like STL wants you to create a heap by first creating a vector, then using make_heap from <algorithm>:

std::vector<SimpleClass> Numbers(8);
// ... populate ...

  // convert Numbers into a heap
  make_heap(Numbers.begin(), Numbers.end()) ;

Why is there not a <heap> just as is there is a <set>, etc?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

Can anyone explain why the last line works? It seems like it is trying to cast a CRectangle as a CTriangle (which I imagine should fail?) but it outputs "5" as the function should.

// virtual members
#include <iostream>
using namespace std;

class CPolygon {
  protected:
    int width, height;
  public:
    void set_values (int a, int b)
      { width=a; height=b; }
    virtual int area () = 0;
      //{ return (0); }
  };

class CRectangle: public CPolygon {
  public:
    int area ()
      { return (width * height); }
  };

class CTriangle: public CPolygon {
  public:
    int area ()
      { return (width * height / 2); }
      int triFunc(){return 5;}
  };

int main () 
{
  CPolygon* ppoly1 = new CRectangle;
  CPolygon* ppoly2 = new CTriangle;
  
  ppoly1->set_values (4,5);
  ppoly2->set_values (4,5);

  cout << ppoly1->area() << endl;
  cout << ppoly2->area() << endl;

std::cout << dynamic_cast<CTriangle*>(ppoly2)->triFunc() << std::endl; //works (and should work)
  std::cout << dynamic_cast<CTriangle*>(ppoly1)->triFunc() << std::endl; //works (but seems like it shouldn't)

  return 0;
}

Thanks,

David