What have you done so far? We don't solve homeworks
chunalt787 commented: thanks for trying to help +1
Sci@phy 97 Posting Whiz in Training
What have you done so far? We don't solve homeworks
Well, you obvioulsy need to increment counter each time you add a node, and decrement it each time you remove a node. It's really not hard, just put it in insert, delete, and create list.
For checking if list is empty... simply check if first==NULL.
But don't you need some wrapper class around everything? This way it's meaningless to write empty() function
And a little piece of advice. When working with decimal numbers, don't use float, instead use more precise double.
Try to see if the problem is input or your class.
Remove all user inputs from main and put something yourself in it. If it works, problem is only in input.
Do you know why do you use typedef?
It's syntax should be:
typedef some huge type A_DEF;
And the code you wrote has no use from typedef since you haven't specified name that will replace that struct.
Funny:
typedef struct node {
char value;
bool isroot;
bool isend;
struct node* sibling;
struct node* child;
}; //<== if semicolon goes here, it's much more reasonable
Ah, now you've said your problem. Solution is simple:
AND && (notice, two & signs)
OR ||
NOT !
Although, if you're doing it for class of some sort, wouldn't it be better to make your own toupper? Just a thought.
Remember, letters are stored actually as ascii code, and you can "add" them or even compare them ('a' > 'A' i think, maybe opposite)
while( chioce != 'n' );}
should actually be: } while( chioce != 'n' );
Have a nice coding :)
Nonetheless, it would not be very clever not to check the return of it.
Ahh, I knew someone will say something like that :)
Of course it can be dangerous, so are pointers (but we still use those).
Also, FYI, see this and this about scanf() . Why instructors insist on teaching bad techniques I still have no idea. Maybe someone will ask sometime and post the response.
For first this I have only one answer: scanf("%40s", string);
For second this: yeah, you got me there, but I'm not talking how scanf is very cool, just that it CAN do a lot of things.
And yet, scanf can be clever function:
#include <stdio.h>
int main(){
char dump, string[40];
printf("Enter whole sentece (yeah, bring spaces too, I can handle it):\n");
scanf ("%[^\n]", string);
scanf("%c", &dump);
printf ("You entered: %s", string);
return 0;
}
Remember that array is double pointer, just like ar. You cannot write ar[x], it is 2D array: ar[x][y]!
I'm using Visual C++ and doesnt give me any compilation error it keeps giving me a run time error after i enter the 1st number i can't figure out whats wrong :S help?[/QUOTE]
The only reason this code should give you error is if you input something else that float, let's say letter. So please post what number did you entered for its first input?
And please, write:
int main()
instead of simply
main()
Please mark this thread solved then
*cough*
Dev-Cpp
THIS compiled ok on dev-cpp:
int main()
{
DWORD dwThreadId;
HANDLE myThread;
void *ptr=NULL;
myThread = CreateThread(
NULL,
0,
MyThreadFunction,
ptr,
0,
&dwThreadId);
while(1){
Sleep(3);
std::cout << "main";
}
std::cin.get();
return 0;
}
notice the void* ptr;
I suspect problem is in your "HELLO", not in dwThreadId
[edit]And Narue's code compiles on Dev :)[/edit]
You could write your hex-to-deci function, and then you can simply write char a = hex-to-deci-function(hex_num)
If you want to determine n-th digit (from the back, so second digit in 1234 is 3) in a number, you do this:
a = number%(10^n) (now your digit is in first place)
digit = a/(10^(n-1)) (now everything is stripped except your digit)
That's the algorithm
@Narue
I see you are using goto
.
And I know programmers don't like to use goto, so if I may ask: why? :)
Did you just use it because it's fastest to write code like that?
Or would you still use it because it has more positive than negative sideeffects in this code?
You have to pass variables as an argument.
If you want to pass one argument of type char to a function that returns bool you declare function: bool Foo(char a_char)
And you call that function like (let's say you already have char letter inside main): Foo(letter);
BUT, if you want to catch (to see) what your function RETURNS, then write this: bool fnc_returned = Foo(letter);
I hope you won't have a problem implementing this in your code. Have fun :)
int a[m][m]
int *ptr = a;
int **ptr = ptr[m];
This doesn't make any sense. If you have to assign dynamic memory, you cannot type int a[m][m];
.
Yes, you have to use pointers. Don't worry, they're not too scary.
And using 2D array of pointers is a little itchy.
First, you have to create pointer to array of pointers of size m:
int** array = new int*[m];
Now you have "outer shell, but you need for each pointer-to-pointer to make m new pointers to int:
for (int i = 0; i < m; i++)
array[i] = new int[m];
And that's it. After that you can use it just like array: array[i][j];
etc.
Hope to help :)
And what is the question?
Um, ok... I'll give you a hint... something about your for loop is wrong. It will always return that number is prime. Try to backtrack you program if you enter for example number 4. It shouldn't be prime, but... :)
And for all those if-statements... try using some else if too
It goes like this:
n(0) = 1;
n(1) = 1;
n(2) = n(1), n(2) //so it's actually two numbers
n(3) = n(1), n(2), n(3)...
Bottom line is: First two numbers just copy.
The print others in a for loop
And what does
fflush(NULL)
do?
Flushes everything (like a damn good toilet :) )
Just some literature from wikipedia:
"In C99, there is a bool type, along with the values true and false, defined in the <stdbool.h>"
Here's what I came up with:
//a lot of code
Here are the 2 errors- error C2296: '+' : illegal, left operand has type 'int (__cdecl *)(int)' and cpp(27) : error C2082: redefinition of formal parameter 'i'
On line 22 you are declaring function. There's no semicolon in the end.
Main is also a function. Do you write: int main();
It shouldn't happen, but this code isn't giving anything
and sorry but this gives error too
ifstream& FileHandler::getFileIn()const{ return fileIn; }
compile error:
invalid initialization of reference of type 'std::ofstream&' from expression of type 'const std::ofstream'
Huh? ifstream function cannot give ofstream error.
Be sure that you've changed declarations in header file too!
And no, you don't need to make private variables references. Only the function returns reference
Like twomers said, just try to return them as references:
ifstream& FileHandler::getFileIn()const{
return fileIn;
}
Post a small code where you are using your function, please
Hi,
When I used scanf instead of scanf_s, it printed the output properly... I am not aware of scanf_s... googled it and found it out its some secure version of scanf or something... i use unix and this is available only for MS C I guess...scanf("%d %d %d %d ", &a1,&a2, &a3, &a4);
I doubt that IT printed out properly. Maybe you retyped it, because on my compiler (gcc) problem is with blank space after last %d that exists in his code, and so scanf asks for continuation of input... Or something :/ pretty weird function
Zip and post entire code, please.
It's frustrating me now too :)
First: this is not C++ program!
Second: scanf_s - what kind of function is that? I don't know, maybe it exists, but I've heard only of scanf.
Third: solution is simple, remove blank space between last %d and "
scanf_s("%d %d %d %d", &a1,&a2, &a3, &a4);
// not (%d "), but (%d")
Fourth: Don't write void main()
! Write int main()
instead.
Well the definition is within the class and classes can access their own private data members so ya it should work. I tried putting it in the public section just to check and it did the same thing. As to your example thats basically the same thing that im trying to do. I don't see why mine doesnt work.
I'm not sure about this.
Function can access IT'S own members (this->memb1; this->memb2;)
But I'm really not sure about accessing other instance of same type...
Maybe your problem is in something else...
I am not sure about this , But Why returning (*this)? In your = operator.
(*this) is returned so you can write things like:
myObjA = myObjB = myObjC;
Are you sure this code works:
ll = queue.ll;
'll' is under private section, right? I'm not sure if you can access it.
Here's my code example if it helps:
//operators
//=
Complex& Complex::operator=(Complex const& aCplx){
if (this != &aCplx){
mNum.Im = aCplx.getIm();
mNum.Re = aCplx.getRe();
}
return *this;
}
Some changes:
#include <iostream>
using namespace std;
const int MAX = 5, MAX2 = 3;
class notebook {
int num;
public :
notebook (); //this is wrong. Either write some constructor or don't write it (use default)
void set_num( int n ) { num = n; }
int get_num() { return num; }
};
int main()
{
notebook Num_Of_Notebook[ MAX ][ MAX2 ] = {};
int unitNumber = 0;
for (int i = 0; i < MAX; i++) //missing bracket!
cout << "Notebook Model " << i + 1;
for (int j = 0; j < MAX2; j++) {
cout << "Branch " << j + 1;
Num_Of_Notebook[ i ][ j ].set_num( unitNumber ); }
cin.get();
cin.ignore();
return 0;
}
You obviously have three options about kwh, and using if-else is wrong approach.
Instead use:
if (kwh <= 20){
//do some coding here
}
else if (kwh > 20 && kwh <= 40){
//do other code here
//notice that I wrote here kwh > 20, not kwh >= 20
//that's because kwh == 20 is included already
}
else{
//some code here if it's not in first two
//(so it's obviously kwh > 40)
}
I made slight adjustment:
sky.erase(sky.begin()+x);
More on: http://www.cplusplus.com/reference/string/string/erase.html
EDIT: oh yes, x <= sky.size() is wrong, should be x < sky.size()
Simplyfied, there is some "GODlike" program that starts your C++ code:
int a = main();
//do stuff with a
If you write "void main()" variable a will be garbage.
MOST of the time, it doesn't matter, but it's better not to go against GODlike program :)
More info on:
http://www.gidnetwork.com/b-66.html
Ok, and you have just one slight error that programmers freak about. Instead of writing void main()
, consider using int main()
.
//start printing numbers
for (i = starting_num; i <= ending_num; i++){
cout<<i;
cout<<" " //this prints separation between two numbers
}
Separate printout to the screen in:
dots before numbers and number.
So, first, if you are in row 0 (first one) the dots should be invert from that (like some_value - nr_of_row), so if you go down the dots should decrease (some_value you have to figure out yourself).
And after dots just print numbers with some_other_value space after them
You need two for-loops.
First one (outer) should count rows, while inner one should (depending of the row it's in) count columns
How do you assing memory to each pointer?
How's that a programming question?
Here:
1 2 3 4
. 6 7 8
. . 1 2
. . . 6
That's uper triangle
It has already been pointed out to you.
if (genArray[i] == "ACG")
This will not work, your are comparing a char* with a string. As it has already been pointed out.I'll just restate the fact you need to check character by character
if (genArray[i] == "A"){ if (genArray[i+1] == "C"){ if (genArray[i+2] == "G"){ std::cout << "Found 1!" } } }
very simple way
And of course, to write 'A', 'C', 'G', not "A", "C", "G"
What is this:
if (genArray[i] == "ACG")
genArray == some_character, not some_string!
The code isn't that simple... You have to check char by char if 'A' then 'C' then 'G' occurs!
And of course, typos that Salem told you :)
Hi Everyone,
I know conio.h is not available in Unix. I want to use getch(). Using curses.h needs causes the screen to clear which I don't want.
I found a code snippet (source : internet) using termios, it works, the thing is I need to press enter/or any other key thrice...
#include <stdio.h> #include <termios.h> #include <unistd.h> int mygetch ( void ) { int ch; struct termios oldt, newt; tcgetattr ( STDIN_FILENO, &oldt ); newt = oldt; newt.c_lflag &= ~( ICANON | ECHO ); tcsetattr ( STDIN_FILENO, TCSANOW, &newt ); ch = getchar(); tcsetattr ( STDIN_FILENO, TCSANOW, &oldt ); return ch; }
Any help on this would be of great help...
This exact code works for me perfectly
Two problems that I see.
What about your input code? First you ask user to insert "a and n", but using scanf() you first get n, then a. That could be problem.
And on end of int main() add return 0;