daviddoria 334 Posting Virtuoso Featured Poster

Search sounds like a good way to go. I don't know what you mean by it uses "an array to search", but this shouldn't use any extra memory, it looks you just specify a range of indices which is your search patter, and a range of indices in which to search.

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

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

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

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

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

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

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

alex,

Us telling you exactly where to put each character is not helpful for anyone involved. I'd suggest finding a basic c++ tutorial dealing with functions, arguments, and parameters. Compiling a few basic things and playing with them will help you get a handle on things. Then we can return to your vector question if you still have a problem.

David

daviddoria 334 Posting Virtuoso Featured Poster

Looks good. You just have to get the Map variable into the draw_map function.

daviddoria 334 Posting Virtuoso Featured Poster
std::vector<std::vector<int> > yourGrid;
msize(3,3,yourGrid);
daviddoria 334 Posting Virtuoso Featured Poster

You get to do

yourMap[i][j] = an int;

(Just like a 2d array)

daviddoria 334 Posting Virtuoso Featured Poster

I didn't see this post before responding in your other post.

I'd recommend using a std::vector<std::vector<int> > . You can then resize on the fly.

(not tested... just a guideline)

void msize(int Height,int Width,  std::vector<std::vector<int> > map)
{

map.resize(width); // you'll have to pick if the outer vector or the inner vectors are the rows or cols
for(unsigned int i = 0; i < map.size(); i++)
  {
  map[i].resize(height);
  }

}

David

daviddoria 334 Posting Virtuoso Featured Poster

1) Instead of

if (convert_code == "f")
    {
        c_f_fraction();
    }

you'd want to do:

main()
{
  if (convert_code == "f")
    {
    std::cout << "\nPlease input the numerator of the fraction you wish to convert.<#/help> ";
    std::cin >> numerator;
    std::cout << "\nPlease input the denominator of the fraction you wish to convert.<#/help> ";
    std::cin >> denominator;
        c_f_fraction(numerator, denominator);
    }
}

double c_f_fraction(double numerator, double denominator)
{
  // your logic here (something like return numerator/denominator;
}

See what I mean? This way the function is much more reusable (if you read numbers from a file, or generated them manually, you can still convert them (rather than relying on user input to happen in every instance).

3) The idea is "documentation by good code writing". The problem is that you have to assume programmers will not keep the documentation up to date (that is, they will change the code but forget to update the documentation). So,you have to train them to write the code in such a way that it is "self documenting". The use of intelligible variable names is a good start.

daviddoria 334 Posting Virtuoso Featured Poster

Why are you reading them as ints? You should change:

int user;
int password;

to

std::string user;
std::string password;

and

if (user1 == user)

to

if (user1.compare(user) == 0)
daviddoria 334 Posting Virtuoso Featured Poster

A few comments:

1) You should handle all of the user input directly in main and then pass values to the functions (rather than call the functions with no arguments and do the input inside as you have done).

2) Rather than have a conversion from each type to every other one (imagine how complicated this would get if there were 10 types instead of 3!), I would always convert to a "common" format (say always to decimal). Then you can have a single function for each type to convert from the common type. You will then have ~2N functions vs ~N^2 (where N is the number of types)

3) Use descriptive variable and function names! c_f_fraction_1 is not obvious to a first time reader of the code.

Good luck,

David

daviddoria 334 Posting Virtuoso Featured Poster

I think it is because your declaration does not take the variables by reference.

Try changing

void reshuffle (std::string::size_type,std::string::size_type,std::string);

to

void reshuffle (std::string::size_type&,std::string::size_type&,std::string&);
daviddoria 334 Posting Virtuoso Featured Poster

Please fix your indentation, it is crazy!

Also, we may need to see the contents of CAMERA.h

daviddoria 334 Posting Virtuoso Featured Poster

The ifstream should automatically parse on white space. That is:

dataFIle >> user >> password;

should do the trick.

daviddoria 334 Posting Virtuoso Featured Poster

I don't know how to compare char arrays, I've never tried :)

std::vector<std::string>
daviddoria 334 Posting Virtuoso Featured Poster

If you used std::string instead of char arrays, you could use the .Compare function: http://programmingexamples.net/index.php?title=CPP/Strings/Compare

daviddoria 334 Posting Virtuoso Featured Poster

You have declared tokens as a pointer to a vector:

std::vector<char*>* tokens

But are then trying to access the size() function as if it is an object (with the '.' operator):

tokens.size()

You need to instead use the -> operator:

tokens->size()

You also have to use

(*tokens)[i]

instead of

tokens[i]

David

nirali7 commented: thnks a ton... :) +0
daviddoria 334 Posting Virtuoso Featured Poster

You can't put a semicolon at the end if the "if" line. Google should know about plenty of tutorials to get you started.

daviddoria 334 Posting Virtuoso Featured Poster

Please use code tags when you post code. Also, please include the compiler output. "It said something is wrong" is pretty hard for us to debug!

daviddoria 334 Posting Virtuoso Featured Poster

Just store the words in a std::vector<std::string> and then output the appropriate word with std::cout << words[i]; . You could alternatively use a switch statement.

In the future, please use more descriptive thread titles!

David

daviddoria 334 Posting Virtuoso Featured Poster

If you can get the whole input, then put it into a std::stringstream and you can use the >> operator just like you would with an fstream.

Lerner - I think he just means to paste something into the console window and then press enter, just as if he had typed it in the window directly.

David

daviddoria 334 Posting Virtuoso Featured Poster

This is some fancy footwork with some STL stuff, but it does the trick:
http://programmingexamples.net/index.php?title=CPP/Strings/Split

Basically you are trying to separate the paragraph by the ' ' character (a space). Then you can store each word in a std::vector<std::string> and then use yourVector.size() to get a count of the words.

cpeepee commented: thank you very much! +0
daviddoria 334 Posting Virtuoso Featured Poster

Can you explain what the problem is? What is an example input, current output, and the expected output?

David

daviddoria 334 Posting Virtuoso Featured Poster

You are correct - typically you give them the source code and they compile it themselves. If you link against any libraries, they would have to have identical versions of the libraries to run the binaries directly. You would also have the issue of 32 vs 64bit.

daviddoria 334 Posting Virtuoso Featured Poster

And as far as lay-people are concerned, Ubuntu IS Linux. You can't just install Linux, you have to install one of its varieties (Ubuntu, Fedora, etc).

daviddoria 334 Posting Virtuoso Featured Poster

Haha sfuo you beat me to the punch!

daviddoria 334 Posting Virtuoso Featured Poster

I don't know about this stdafx business, but from what my compiler tells me you need cstdio to use printf (but you should use std::cout instead). Also, strcat_s is not necessary, just use std::string and the '+' operator. I would even store the words originally in a std::vector<std::string>. I also corrected the spelling of 'sentence' :)

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


const char SIZE = 100;

int main(int argc, char* argv[])
{
    const char *article[5] = {"the", "a", "one", "some", "any"};
    const char *noun[5] = {"boy", "girl", "dog", "town", "truck"};
    const char *verb[5] = {"drove", "jumped", "ran", "walked", "flew"};
    const char *preposition[5] = {"to", "from", "over", "under", "on"};

  srand((unsigned)time(NULL));

  std::string sentence;
  for(unsigned int i = 0; i <= 20; i++)
  {
      sentence = std::string(article[rand()%5]) + " " +
                 std::string(noun[rand()%5]) + " " +
                 std::string(verb[rand()%5]) + " " +
                 std::string(preposition[rand()%5]) + " " +
                 std::string(article[rand()%5]);
      std::cout << sentence << std::endl;
  }
  
  return 0;
}

Hope that helps.

David

daviddoria 334 Posting Virtuoso Featured Poster

First I would be really careful about calling a variable 'size'. It could definitely clash with other things.

Let me recommend that you add comments to the major portions of your code. This will not only help us read what is going on in your code, but I bet once you translate that code to English you will understand what is going wrong.

daviddoria 334 Posting Virtuoso Featured Poster

Try looking up "function pointers". Does this example help? http://programmingexamples.net/index.php?title=CPP/FunctionPointer

David

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

Haha what I meant was not "mark as solved" on DaniWeb, but a "solved problem" in the research field (i.e. everyone agrees no further work is necessary).

daviddoria 334 Posting Virtuoso Featured Poster

My only suggestion is to not get frustrated - this is a very difficult problem and it is definitely not "solved". People still devise clever tricks tailored to their particular data sets, so you will certainly not be able to find any "one size fits all" algorithm to do something like this.

Good luck!

daviddoria 334 Posting Virtuoso Featured Poster

You will save yourself countless headaches by not using double arrays, but rather double vectors:

http://programmingexamples.net/index.php?title=CPP/2DVector

daviddoria 334 Posting Virtuoso Featured Poster

VTK (vtk.org) and ITK (itk.org) also have a lot of tools that could be useful. VTK even has a huge set of examples :) http://www.vtk.org/Wiki/VTK/Examples/Cxx

Lusiphur commented: Useful links :) +1
daviddoria 334 Posting Virtuoso Featured Poster

Also, please use code tags when posting code.

daviddoria 334 Posting Virtuoso Featured Poster

That garbage value is most likely caused by an uninitialized variable.

Any time I see '**':

neuron** connections;

I cringe...

Have you considered using:

std::vector<std::vector<neuron> > connections;

instead?