necrolin 94 Posting Whiz in Training

According to Bruce Eckel the performance difference between C++ and C is usually less than +/-10%. He also noted that a well designed C++ program can be "more efficient than the C counterpart".... aka it can be faster. In any case we can call them relatively equal... depending on the quality of the code that's being tested.

necrolin 94 Posting Whiz in Training

I'm lazy so I'll copy and paste from "Beginning Visual C++ 2005" (I have the pdf)

When you create a new project workspace, Visual C++ 2005 automatically creates configurations for pro-
ducing two versions of your application. One version, called the Debug version, includes information
that helps you debug the program. With the Debug version of your program you can step through the
code when things go wrong, checking on the data values in the program. The other, called the Release
version, has no debug information included and has the code optimization options for the compiler
turned on to provide you with the most efficient executable module.

necrolin 94 Posting Whiz in Training

If I have 2 classes

a) class Business
b) class Customer

I want the Business class to be able to store a dynamic list of it's customers. So I thought the easiest way to do this would be to use a vector.

class Business
{
    vector<Customer> customers;
public:
    Business();
};

Business::Business()
{
    //I want to be do something like this, but it's not working
    customers.push_back(Customer("Name1"));

}

My question is how do I get Customer objects into my vector?

necrolin 94 Posting Whiz in Training
lookin dat dar string
    izzit dat vowuel?
        yup, stop lookin'
        nope, keepa goin'

didja find it?
    yup, hola!
    nope, hola louda!

LMAO, I love it. It's humorous and it gives the correct answer to the question.

To add some more insight into the answer I would put the vowels into a container like an array or vector. Then just loop through it one letter at a time. Tom's algorithm is correct, just translate it into C/C++.

for (int i = 0; i < mystring.length(); i++)
{
   for (int j = 0; j < myArrayLength; j++)
   {
         if (mystring[i] == myArray[j])
         {
             //it's a vowel
          }
    }
}
necrolin 94 Posting Whiz in Training

See below.

necrolin 94 Posting Whiz in Training

This one works, not super elegant, but it works. You'll have to eliminate your marker if you want.

#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>

using namespace std;

int main(void)
{
 char s[50];
 cout << "\nEnter String to Remove Spaces from: " << endl;
 gets(s);
 int len = strlen(s),j=0;

//This will keep track of where we are
 int currentPosition = 0;

 //Clear left side only, stop at a '*' character
 for(int i = 0 ; i < len ; i++)
 {
     //Declared before the for loop so it will persist after loop ends
     currentPosition = i;
     
    if(isspace(s[i]))
   {
     j=i;
     for(;j<len-1;j++)
      s[j] = s[j+1];

     if(i!=j)
      {
       s[j] = '\0';
       len--;
      }

     if (s[i] == '*')
     {
         break;
     }
   }
 }

 //Clear right ride from the '*' character
 for(int i = currentPosition ; i < len; i++)
 {
    if(isspace(s[i]))
   {
     j=i;
     for(;j<len-1;j++)
      s[j] = s[j+1];

     if(i!=j)
      {
       s[j] = '\0';
       len--;
      }
   }
 }

 cout << "\n\nNew Sting After Removing Spaces: " << s << endl;

 cout << "Program paused...";
 cin.get();

 return 0;
}
necrolin 94 Posting Whiz in Training

yeah, thats exactly what i am trying to do, its sounds simple, but the problem is i don't really know how to implement it in the code itself. Could you provide me with some code, please. Thanks.

Method 1 is to use a "middle" variable.

#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>

using namespace std;

int main(void)
{
 char s[50];
 cout << "\nEnter String to Remove Spaces from: " << endl;
 gets(s);
 int len = strlen(s),j=0;
 int middle = len / 2;

 //Clear left side only
 for(int i = 0 ; i < middle; i++)
 {
    if(isspace(s[i]))
   {
     j=i;
     for(;j<len-1;j++)
      s[j] = s[j+1];

     if(i!=j)
      {
       s[j] = '\0';
       len--;
      }
   }
 }

 //Clear right ride
 for(int i = middle ; i < len; i++)
 {
    if(isspace(s[i]))
   {
     j=i;
     for(;j<len-1;j++)
      s[j] = s[j+1];

     if(i!=j)
      {
       s[j] = '\0';
       len--;
      }
   }
 }

 cout << "\n\nNew Sting After Removing Spaces: " << s << endl;

 cout << "Program paused...";
 cin.get();

 return 0;
}

Note that I replaced system("pause"); with cin.get() because I'm using Unix and I wanted a platform independent way of pausing the program.

necrolin 94 Posting Whiz in Training

If I understand you correctly then you want to remove the spaces in a string in two passes. In one pass you remove the spaces from the left half of the string and then in the second pass you remove the spaces from the right side of the string. The easiest way to do this would be to use the length of the string as the limiting agent by finding the middle of the string. You already have the length of the string, variable len, so why not use a middle variable? int middle = len / 2; Then loop one side until the middle, then the next side from the middle to the end.

You mention something about a breaking point string, but I'm not too sure what you're getting at there. If you need to stop at a specific character then I suppose you can put that right into your loop.

necrolin 94 Posting Whiz in Training

I can't think of any really simple way to do this. I suppose you could use something like a struct or a union. This way you could store more than one type of variable depending on what you need. I was thinking something like:

#include <iostream>

using namespace std;

struct Data
{
	int a;
	char b;
	Data();
};

Data::Data()
{
	a = 0;
	b = '\0';
}

int main()
{
	Data data;
	string temp;
	
	cout << "Enter some data -> ";
	cin >> temp;
	
	if (temp == "4")
	{
		data.a = 4;
	}
	else
	{
		data.b = '*';
	}
	
	cout << data.a << endl << data.b << endl;
	
	return 0;
}

Not an ideal solution, but it works.

necrolin 94 Posting Whiz in Training

it was an actual assignment and the lecturer doesn't really care.

Your teacher doesn't care, but maybe you should. You got someone else to do for homework for you. Congratulations, you missed out on the chance to learn something from your own effort. Now what happens come exam time? Will you bring a laptop in to the exam and ask someone else to write it for you? Forums like this should be a place where you get help solving problems that you can't solve on your own and not a means of getting out of doing your own work. Just my 2 cents so you don't have to agree with me.

necrolin 94 Posting Whiz in Training

"Much to my disappointment, though, my built-in Broadcom wireless network adapter wasn't supported by default."

What you're saying is that you had to install the driver. OK... big deal? What I like about Linux is that you can install the Broadcom drivers from the repository. No CDs required. Have you ever tried to re-install Windows for a friend who "thinks" he has all his original CDs for the computer just to find out that he's missing the one that has drivers for the MOBO (built in network card, etc)?? So, you search the manufacturers website just to find out that it has all the drivers listed except the one you need. Now that's a pain in the behind. It's not that I disagree with you on the tweaking part. Linux has been given me it's fair share of stress, but I do think that your example is rather weak.

necrolin 94 Posting Whiz in Training

"gNewSense contains only free software. It's also the distro that Stallman himself uses--how can you beat that?"

Those are two good reasons not to use it. Until free software works on my hardware and actually allows me to do what I need it to do it's a no go on my computers. I have listened to RMS and I personally disagree with 1/2 of what he says. So, no thanks. I'll pass on this one.

necrolin 94 Posting Whiz in Training

You said: "Help Me With this please...I'm just learning C++", but you didn't say what you want help with. Where are you stuck?

Check for even numbers: variable % 2 == 0

Chick for highest number: just use a variable to store the highest value and use an if statement to compare the present value to the highest value.

Average: I'm sure you can work out how to calculate an average so I won't bother with this one.

If you need more specific help then you'll have to be more specific in your question....

necrolin 94 Posting Whiz in Training

iostream allows you to use cout, cin, etc.
cstdlib allows you to use system(), exit(), EXIT_SUCCESS, etc.

Basically, when you use the #include statement you are adding the ability to use functions/code which were previously written and are located somewhere else in your system, such as in a separate file or in a library.

necrolin 94 Posting Whiz in Training

Looks like pdcurses need to be copied somewhere into the mingw directory. If you look at this page:

http://www.mingw.org/wiki/LibraryPathHOWTO

it should give you more information. In any case, from a quick glance it looks like mingw uses /mingw/mingw32/lib and /mingw/lib directories to store libraries it uses. I'd assume that you would copy the files to one of these directories. Sorry if I'm not much help. I've never used ncurses so I'm just googling so that I can learn something myself.

For Ubuntu, I believe that you'd need the ncurses development libraries as well. Use synaptic and run a search on ncurses. Looks like I have it installed as ncurses5-dev.

necrolin 94 Posting Whiz in Training

The program is using something called binary shifting. For example:

1 << 1

one is binary "001" shifted by one equals binary 010, aka 2.

1 << 2

Binary 001 shifted by two is binary 100, aka 4.

So, if we assume that bin == 100 then our first iteration of the for loop would calculate:
x = bin / (1 << y);
x = 100 / (1 << 7);
x = 100 / 10000000;
x = 0; //Since x is an int the decimal portion is dropped.

bin = bin - x * (1 << y);
bin = 100 - 0 * (1 << 7);
bin = 100 - 0;
bin = 100;

Your program repeats this 7 times, but each time the shift is smaller.

If you want a simple example of converting decimal to binary you can check out Bruce Eckel's book "Thinking in C++". In chapter 3 they have a section on shift operators. You can download the book for free from:

http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

necrolin 94 Posting Whiz in Training

Google is your friend. Apparently ncurses is available in Mingw as a user contributed library/package called pdcurses. You can get it....

http://sourceforge.net/projects/mingw/files/User%20Contributed_%20pdcurses/

...by clicking on the above link. Mind you I've never used this package, but it looks like what you're looking for. Hope it helps.

necrolin 94 Posting Whiz in Training

Why don't you write it out in English and then translate it into C? Just write down what you see in each row, 1 row at a time.

If it's row 1 or row 2 then the color is blue
..
if it's row 3 and the column is 3 or 4 then the color is green, else the color is blue
...
etc.

Notice that my statements can be translated very easily into if statements. You'll just need 2 for loops to loop through every row & column and select the right color.

necrolin 94 Posting Whiz in Training

I'm not actually looking for a quick answer to my homework. That's why I asked this question because I was hoping that I could find a program that would display the whole calendar. Right now the program I made is just displaying one month and I don't know how to let it display the 12 months. Can anyone help?

If you pay me a million dollars I'll do your homework for you. Otherwise post the code you already have and I'm sure we can help you finish your homework.

necrolin 94 Posting Whiz in Training

You do realize that == compares two values whereas = assigns a value. Therefore, it's...

t = a;
a = b;
b = t;

necrolin 94 Posting Whiz in Training

The way that you do this exercise is you bust out your handy dandy computer, use your problem solving skills to work it out yourself, try to compile the program, and if you still have problems you ask on a forum. You've just posted 3 of your homework assignments with absolutely no code and no effort put into doing them yourself. Common what are you trying to do? It's your homework not ours.

necrolin 94 Posting Whiz in Training

I think you need to be a little more specific. What do you want to accomplish?

necrolin 94 Posting Whiz in Training

OK, so if you imaging working with a spreadsheet then you could arrange the data as such:

a1, a2, a3
b1, b2, b3
c1, c2, c3
...etc

So in an array this would be:

intArray[0][0], intArray[0][1], intArray[0][2]
intArray[1][0], intArray[1][1], intArray[1][2]
intArray[2][0], intArray[2][1], intArray[2][2]
...etc

That's of course just one way to arrange the data and it would arrange the data exactly as it is in the input file. For some c++ code that could accomplish this take a look at my example (note that I don't test the input which isn't a great idea, but it's just meant as an example and not as a final product. Hope it helps you understand.

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    // MAX represents the maximum size of the array
    const int MAX = 3;

    // 3 x 3 array
    int intArray[MAX][MAX];

    //Open the file
    ifstream infile;
    infile.open("data.txt");

    if (infile.fail())
    {
        cout << "Opening the file failed\n";
        exit(1);
    }

    //Loop through the array. First for loop is for rows, second is for columns
    for (int i = 0; i < MAX; i++)
    {
        for (int x = 0; x < MAX; x++)
        {
            infile >> intArray[i][x];

            //Display Output exactly like it was input into the program
            if (x < 2)
            {
                cout << intArray[i][x] << " ";
            }
            else
            {
                cout << intArray[i][x] << endl; //add a line break after every 3rd digit
            }
        }
    } …
necrolin 94 Posting Whiz in Training

Now you're not incrementing either the column nor the row??!! A multi-dimensional array is pretty much exactly the same as a single-dimensional array where you can record data from myArray[0][0] to myArray[MAX][MAX]. So increment them both as needed.

I have to go out right now, but when I get back I'll give you an example. Look at your while() loop. Carefully think about what you want to be doing there. I'll help you more when I get back in an hour or two.

necrolin 94 Posting Whiz in Training

OK, so you stated:

"First line = candidates
second line = region
after those two it starts off as 3 1 12 first is candidate, region, amount of votes."

Therefore, first you want to read in the # of candidates. This number you'll use to control your loops when displaying the contents of your array. This will be a control for the maximum number of rows or columns, whatever you decide to make it.

Next, read the # of regions. Again use this for your loops.

Lastly, you want to choose some sane way of representing the data your array. You have 10 columns and 20 rows to work with. Organize the data the way you would in a spreadsheet with 10 columns and 20 rows.

necrolin 94 Posting Whiz in Training

Also, remember that a multiple dimensional array has rows and columns, like a spreadsheet. When you run:

a[cnt][NCOL] = x;

You're saying:
a[0][10] = x;
a[1][10] = x;
a[2][10] = x;
...etc.

Notice how you're only filling it one dimensionally. You're only using column 10. Then you do.

for(int i = 0; i < x; ++i) {
for(int j = 0; j < x; ++j) 
cout << setw(3) << a[i][j];
cout << endl;
}

In this code you're incrementing both rows and columns, but your columns haven't been initialized yet so you'll be printing radom garbage.

necrolin 94 Posting Whiz in Training
while(infile && cnt < a[NROW][NCOL]){
a[cnt][NCOL] = x;
++cnt;
infile >> x;
}

You're while test is wrong. a[NROW][NCOL] has some random value stored in it because it hasn't been initialized yet, same as a it would in a simple one dimensional array. So, what you're saying is "while cnt is smaller than some random number (maybe 15889)" do something. Where what you want to say is:

while (infile && cnt < NROW) { do something }

necrolin 94 Posting Whiz in Training

The "const" at the end of your first error line is your problem. The member function is declared constant so it won't allow you to make any changes to variables in the function.

necrolin 94 Posting Whiz in Training

Nevermind, see you already have a nice answer. I guess I took too long on the response... hehehe... I was thinking you were doing command line and not a Windows GUI program so my suggestion was going to be c style printf()... but I see that's not quite what you were looking for.

#include <cstdio>
printf("Hello, %s. How are you?", "Billy Bob");

necrolin 94 Posting Whiz in Training

Do you mean like this?

string name = "Billy Bob";
cout << "Hello " << name << " you're a wonderful person..." << endl;
necrolin 94 Posting Whiz in Training

Like the others already stated this error comes from trying to manipulate a constant. This should make you wonder a few things including:

Why am I trying to manipulate a constant? It totally beats the point of making something constant if you're going to decrement it later.

If you have to decrement the constant then why not just remove the const and make it a normal variable?

However, if you really need to do this and there's some logical reason for it then there's always const_cast<>. Take a look at my example:

int main()
{
   const int myVar = 5;
   int* j = const_cast<int*>(&myVar);
   *j = *j - 1;
   cout << *j << ": as you can see it's been decremented to 4.\n";

   return 0;
}
necrolin 94 Posting Whiz in Training

You'll need 2 for loops (nested). 1 will be for the width and one for the height.

for (int column = 1; column < n; column++) {
    for (int row = 1; row < n; row++){}
}

Inside the second for loop (rows) you'll have to implement the formulas they gave you in the book/assignment. Use something like if statements to calculate whether it should print a ^ or a #.

necrolin 94 Posting Whiz in Training

The program is supposed to start at main(). Classes on the other hand don't do anything until you create an object and start executing member functions. So, main() would never get executed. Also, remember that classes default to "private:" meaning that only class N would have access to main().

necrolin 94 Posting Whiz in Training

setw(10) << "Agent's Commission".....

Your problem is in your setw(). Width is set to 10 characters but "Agent's Commission" is like 18 characters..... which beats the point of putting setw(10) in there doesn't it? Set the width so that the output actually fits in the width because right now your columns are too small so your formatting gets messed up. You'll probably want to set variable width columns depending on the size of the columns title.

Also:
cout << setfill('*')
cout << fixed << showpoint

.... where's the ";" at the end of the line?? =P

necrolin 94 Posting Whiz in Training

Why is main() inside class N???

By the way your code compiles and works on Solaris, but only if main() is removed from the class. Just copy 'n' past MattyRobot's code into your IDE and see for yourself. ;)

necrolin 94 Posting Whiz in Training

I suppose you could go for a really easy, but inefficient way of sorting by using 2 arrays. One could be your source array and the other would be your destination. Then just keep on looping though your source array. In loop 1 find the smallest number and copy it into your destination array. In loop 2 find the second smallest number, etc until you finish.

necrolin 94 Posting Whiz in Training

The only error in this file is horrible formatting. The program compiles and runs just fine.

necrolin 94 Posting Whiz in Training

With getline() you'll basically end up with a string containing all the numbers on the line. Then you can use an istringstream to convert each string in your line to the corresponding number individually. It'll work, but it just seems like the hard way to solve this problem. Take a look at my example program to see what I was thinking of.

//Read random number of ints from a file

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int main()
{
	//Open file for reading, adjust filename to a real file
	ifstream infile;
	infile.open("data.txt");
	
	int temporary;
	vector<int> numbers;
	
	//Loop through file and fill in the vector
	while (infile >> temporary)
	{
		numbers.push_back(temporary); //add to end of vector
	}
	
	//Display contents of vector to screen
	for (int i = 0; i < numbers.size(); i++)
	{
		cout << "Number " << i << " -> " << numbers[i] << endl;
	}
	
	//Clean up and exit
	infile.close();
	return 0;
}

/* Output of program
 * 
 * Number 0 -> 1
 * Number 1 -> 3
 * Number 2 -> 5
 * Number 3 -> 9
 * Number 4 -> 88
 * /

Hope this helps you. If you have any questions about the code then feel free to shoot away.

necrolin 94 Posting Whiz in Training

There are 2 versions of Code::Blocks. One comes with a compiler and the other one doesn't. VermonDozier is right about Mingw though. Both Code::Blocks and DevC++ use it. However, since DevC++ is an abandoned project that hasn't seen any tender caring love in years the version of Mingw that comes with Code::Blocks will be newer. If you're interested in having the latest Mingw you can always pick it up from their website at: http://www.mingw.org/.

Have fun with your coding. =)

necrolin 94 Posting Whiz in Training

There's more than one way to do this. I think that since you don't know how many numbers there are on that line then the easiest way to achieve this would be to use an integer vector and a while loop. While there's data on the line add it to the vector. This way you have easy access to storage for a random number of integers and an easy way to access and manipulate them.

necrolin 94 Posting Whiz in Training

I find it frustrating when people ask for help and then don't read the help that they've received only to ask the same question over again...

When you code an application and it reaches it's end it closes. Meaning the terminal window will close and you will not see your message. Therefore, like a few people already suggested you must PAUSE the application via the line: cin.get();

Visual Studio is overkill for someone who is just starting out with hello world type programs. Every programming teacher I've ever had has always told the students not to use a big fancy IDE to learn a language, but to use something simple instead. So, for now forget about visual studio, use Code::Blocks and just add the cin.get() bit at the end of your program (right before the return 0; line). This will allow you to see your output and you won't have to arm wrestle with VS until you've written a few programs and are ready to tackle it. Just my 2 cents worth.

necrolin 94 Posting Whiz in Training

I've used both for a while and I have to say that Win7 is by far the best OS Microsoft has made to date. It's really really nice. It has a lot of really useful improvements in the UI and Win 7 seems to be a lot snappier on the same machine than Vista. I haven't done any benchmarking but I'd be willing to bet $100 that it's faster on my machine. If you're moving from XP then get ready for a big change in the interface. But without a doubt if I had a choice I'd go with 7.

necrolin 94 Posting Whiz in Training

Never mind. I found a good explanation on the net.

necrolin 94 Posting Whiz in Training

What's the difference between (aside from syntax I mean):

a)
typedef struct MyStruct{....};

b)
typedef struct{...}MyStruct;

I saw this example in "Thinking in C++", but there's no real explanation as to if they're exactly the same or if there's some real difference in them.

necrolin 94 Posting Whiz in Training

Ancient Dragon actually made a very valid point. When things don't work in Linux it feels like you're banging your head against a brick wall. It's been my main OS for almost 10 years, but there are still things here and there that make me go absolutely crazy.

It's funny though because I recently had the same exact experience as AD. I wanted to do some C++ programming in Linux and had a hard time choosing between gtkmm and wxwidgets because I knew nothing about them. I installed the dev packages and I couldn't get even a basic hello world program to compile in either. It literally took HOURS to figure out what libraries were missing. In Windows when you install a program it comes with everything you need. I could have probably compiled the same hello world program in 10 minutes in Windows using the same wxwidgets or gtkmm libraries. So there's no need to get defensive Linux users. There are things in Linux which are great and there are things in Linux that absolutely and completely suck. That's just the way it is.

necrolin 94 Posting Whiz in Training

How about buggiest Linux distro of 2009. My vote goes to OpenSuse 11.1. They should just call it Linux ME.

necrolin 94 Posting Whiz in Training

Correct me if I'm wrong, but wouldn't this be pseudorandom and return the exact same series every time it's run? The way I was taught was to use a seed like this....

#include <ctime>

int seed = static_cast<int>(time(0));
srand(seed);

int randomNumber = minimum + rand() % (maximum - minimum + 1);

necrolin 94 Posting Whiz in Training

I'm going to assume he means an internet cafe like we have in Asia. A place packed full of computers and a ton of people there to game. This is quite different from the internet cafes which I've seen in north america where they have a coffee shop with a couple of computers for people to surf the net. Linux is most certainly not going to work for the Asian style internet cafe. Even for the north North American style internet cafes I think that if most people saw linux they would just go "huh?? What the hell is this and where is IE??!!". I don't think it's a good idea from a marketing point of view.

necrolin 94 Posting Whiz in Training

I have a problem with this post. When I started reading the post I saw something to the effect of IE opening itself up hundreds of times. This does sound like a virus. However, the poster mentions multiple formats. This is a problem.

A format deletes everything on the machine including viruses. If you had the machine formatted then when you booted it up it was clean. This means that either:

a) You're installing the virus after it's been formatted. It's possible. I had a client of mine who had a Windows "driver" which he downloaded from somewhere on the net for his hardware. It was a trojan that installed loads of poop onto his computer. So, to me if this is a virus then you need to take a really hard look at the software that you're using. Where are you getting the virus from? If it's survived multiple formats then YOU are putting it back on your computer. Even if you fix it there's a good chance it will come right back.

b) I haven't gone through the logs, but a few people have mentioned that your logs look clean. Reformatting would have cleaned up the viruses..... so what's left? Does your keyboard have a little button for launching a web browser? Someone else suggested that it could be a keyboard problem. If it's the keyboard then no amount of formatting will help you and/or virus hunting will kill you.

Just a few thoughts.

necrolin 94 Posting Whiz in Training

Some software tests your hardware and refuses to install if it's minimum requirements aren't met. Minimum requirements are usually the very minimum required to just barely get the program running. If your computer doesn't meet those then don't bother even trying to play. It won't break your computer, but it won't be playable.