Ancient Dragon's example will work for the first elemend of the 2D vector, and you'll probably have to nest it to go through all the rows of the 2D vector.
FireSBurnsmuP commented: Like I said, you might have saved my life. +1
Ancient Dragon's example will work for the first elemend of the 2D vector, and you'll probably have to nest it to go through all the rows of the 2D vector.
The easiest way would probably be to take the input as a string and compare each character to '1' and '0'. This will also help you tell if the user entered something like "101234" or "16oz."
Not quite sure what you mean. Boot Camp is only for Windows, and booting with Linux with/without Windows is best done with rEFIt. Which can handle several (not sure of the limit) OSs.
Unless of course you meant support for booting both Windows XP and Windows Vista...
Ah, didn't realize Bootcamp was for Windows only.
- not being bound to proprietary hardware
- bootcamp having support for >2 OSs (I've heard it doesn't)
If it weren't for that, I might buy it :p
^ is a bitwise XOR operator. It can only be used on primitive integer types.
?: is the ternary operator, so named because it takes three parts (see example below). It's shorthand for an if-else clause.
if(condition)
foo();
else
bar();
// can also be done as:
condition ? foo() : bar();
int row = 0;
int col = 0;
for (row =0; row<theArray.length; row++)
{
for(col = 0; col<theArray[row].length; col++)
{
// no work being done here...
// you should compare theArray[row][col] with what you're
// searching for and you can return if it's a match
}
}
if (row == col) // row and col were the array indices, why compare them?
return false;
else
return true;
You're really close again. You've got the assignment backwards for the first value (it should be a = theArray[0];
). There is one other problem with your code though: it doesn't scale. What you posted will rotate the first 10 items, and the loop will cause the rotation to happen however many times there are items in the array. So if the array had 12 items, the first 10 would get rotated 12 times. And if the array had less than 10 items, you'd get an exception. Here's another way to do it:
- save the first value in a temporary variable
- use a loop to iterate over the list, making theArray = theArray[i+1] (for values of i from 0 to theArray.length-1).
- copy the temporary value into the last item in theArray.
You'll notice that you need to adjust the condition in the for loop this time to have a -1. That's because you're indexing the (i+1)th element, so without the adjustment you'd get an ArrayIndexOutOfBoundsException on the last iteration.
1) Where is _strdate() defined? I'm not finding it...
2) Why are you using <iostream> and <stdio.h> (which should be <cstdio>)?
The #include "myheader.h"
will essentially paste the contents of myheader.h into the top of whatever file it is #included into. So it'll be comiled just fine. In order to get the .cpp file compiled, you can just specify it first on the command line, e.g.
$ g++ myheader.cpp main.cpp
and the linker should take care of everything... I think... (been a li'l while...)
When you run a program, the program starts with the main() function by definition. You need a main() somewhere in your project, and whatever file contains main() should be the one you compile to create your executable. You can just put main in another file (e.g. main.cpp) and that way keep your implementation stuff separate.
Oh, and when I start my computer, I use the power button, not a .o file :lol:
1) Post some code. Unfortunately, crystal balls are a bit out of the budget, so we can't see what's wrong.
2) A 9-year old book is... well, almost antiquated. Consider getting a new one, since C++ has been standardized since your book was published. Some compilers may have problems with old code. If your compiler is nearly as old as your book, I'd recommend getting a new compiler as well.
you might look at strtok() though I don't know that the MSDN page is particularly easy to understand...
[edit:] if you need to use std::string, you could either use the .c_str() method or see if there's a split() function (I don't remember if it has one offhand).
Okay, I've finished learning C++.
HAHAHA:lol:
What can I do with it? Specifically, I want to do some graphics stuff, anything beyond iostream and fstream. What comes after C++? I know it does more than this.
Come up with a project. Once you know what you want to write, either start writing a library for it (if one is needed), or find a library someone else has written and use that.
What exactly is an API? What does it do, how is it different from other things? Does it just let you access hardware abilities?
An API is an interface for (typically) a library. It provides certain functions for you to use, and then it does the nitty-gritty behind the scenes. You don't need to know how the nitty-gritty gets done, and the API won't tell you. It'll just do it.
I've heard of GUI programming, what is it (beyond what it stands for :rolleyes: )?
It's just like it sounds, making graphical user interfaces for your programs.
(...) it run "almost" right... it's loosing every eleventh integer out of .dat file. (...)
This is because it reads the number, then the loop condition fails so it doesn't save the number. I'll let you figure out a way to fix that ;)
I made a few changes to the code, mostly small ones, but here's what I've got atm:
#include <fstream>
#include <iostream>
using namespace std;
const int MAXLEN = 10;
int buffer[MAXLEN];
int pcount;
int ncount;
ifstream f;
void startnumber()
{
pcount = 0; // makes more sense to initialize to 0 (IMHO)
ncount = 0;
f.open("P2355.DAT",ios::in);
}
void finish()
{
f.close();
}
void getnumber(int &num, bool &endfile) // use references instead of pointers
{
int tmpnum;
f >> tmpnum;
while((!f.eof()) && (pcount < MAXLEN)) // MAXLEN - 1 will have you only use 9 spaces.
// If you used <= you'd need the -1.
{
buffer[pcount++] = tmpnum; // since pcount was init to 0, use post-increment
f >> tmpnum;
}
endfile = ((bool)f.eof());
if(!endfile)
num = buffer[ncount++];
}
void printnumber(int num) // just take a value, not a pointer
{
cout << num << endl;
}
int main(void)
{
int num;
bool endfile;
startnumber();
getnumber(num, endfile);
while(!endfile)
{
printnumber(num);
getnumber(num, endfile);
}
return 0;
}
I made myself a file consisting of 35 lines, each containing the line number (1-35). Here's the output when I run the program:
1
2
3
4
5
6
7
8
9
10 // up to here is correct, but this is where the buffer is full and the rest is junk
10
11
0
0
0
0
-1208485396
0
-1208485272
134521200
134521283
134521296
0
0
0
Here's the main problem that I see: once the buffer is full, you read in …
I'd first off recommend using return types instead of pointers. Handling pointers like that (imho) just makes the code messier than it needs to be. You could do it with references instead of pointers as well. And instead of keeping endfile as a variable, just use f.eof()
.
I'd also suggest you try to design the program to not use global variables. While they do make it easier for this size of a project, using them is typically regarded as a bad thing. You could either wrap them in a class (as you attempted before), or pass them around as parameters. You could (and probably should) also make use of return types. For instance, if your functions used return types, you could have int getnumber(fstream& f);
instead of void getnumber(int*, bool*);
. This is a more intuitive design for the function as well ( getnumber
takes a file and gets a number out of it; seems pretty straightforward, no?). And the fstream could be a local variable in main, rather than a global variable.
As a precaution, you should make sure the file is not empty when you get the first number. You have:
startnumber();
getnumber(&num, &endfile);
while(!endfile)
{
printnumber(&num);
getnumber(&num, &endfile);
}
whereas I would recommend something along the lines of:
startnumber();
while(!f.eof())
{ // note: I'd assume that you'd use references and not need the ampersands
getnumber(num, endfile); // endfile could probably be dropped too if you use f.eof()
// previous line could also …
The easiest way would be to put a big loop around the main part. You could have your menu contain an extra option to quit, and just keep doing calculations until they enter that option.
I don't see the reason for the 3rd loop
for(int k = 0; k < 14; k += 2)
cout << endl;
You'll be outputting 7 newlines between rows of stars... is that what you meant to do?