This: [B]else [B](bnr == hnr)[/B][B];[/B][/B]
is not correct, remove the semicolon ( ;
) and (bnr == hnr)
so that it becomes else
csurfer commented: "Did you write it?" Same question came to my mind too !!! ;) +2
This: [B]else [B](bnr == hnr)[/B][B];[/B][/B]
is not correct, remove the semicolon ( ;
) and (bnr == hnr)
so that it becomes else
They add unnecessary complexity?
#include <iostream> #include <string> using namespace std; int main() { string str; while (cin >> str) count++; cout << count << '\n'; }
The word counting will never stop... (unless you terminate your program in an improper way)
Edit:: AD was before me :P
The following:
for(int i=0;i<(int)str.length();i++)
if(isspace(str[i]))
words++;
has to be:
for(int i=0;i<(int)str.length()[B]-1[/B];i++)
if(isspace(str[i]))
words++;
Edit:: No, this won't work either
Would you mind reading this and use code tags ?
Edit:: And something else: migrate your code to standard C++ code (don't use [B]void main[/B]
for example), review this web page carefully :)
Edit:: Tom Gunn's (one post below, #3) is right in saying that it will check for all space characters, look at this
hey guys im currently in school so i wont be able to compile, does anybody know if this would compile?
Yes, I know, I just ran it through my compiler, result: no it doesn't compile, here are the errors, so that you know where to make adjustments in your code:
ttt.cpp: In function `int main()':
ttt.cpp:40: error: jump to case label
ttt.cpp:31: error: crosses initialization of `std::ofstream fileOutput'
ttt.cpp:41: error: jump to case label
ttt.cpp:31: error: crosses initialization of `std::ofstream fileOutput'
ttt.cpp:45: error: jump to case label
ttt.cpp:31: error: crosses initialization of `std::ofstream fileOutput'
ttt.cpp:40: warning: destructor needed for `fileOutput'
ttt.cpp:40: warning: where case label appears here
ttt.cpp:40: warning: (enclose actions of previous case statements requiring dest
ructors in their own scope.)
ttt.cpp:41: warning: destructor needed for `fileOutput'
ttt.cpp:41: warning: where case label appears here
ttt.cpp:45: warning: destructor needed for `fileOutput'
ttt.cpp:45: warning: where case label appears here
ttt.cpp:52:2: warning: no newline at end of file
:P
P.S: Please bear in mind that Daniweb is no remote compiler service or something :P
thanks for ur reply Sir.
can u tell me from where i can download this tool to convery IDL(Interactive data language) to C++ language.
thanks again
Yes, it's included with omniORB, you can get it here :)
After installing, follow jencas' instructions:
If I remember correctly I wrote: "omniORB contains a tool..."
The name of this command line tool is omniidl.exe and translates your .idl to .cpp (if provided with appropriate parameters)
And why 49 and 57 instead of '0' and '9'??? Are you training for an obfuscation contest?
Agreed, '0' and '9' makes your code more readable, but the OP started with using the ASCII codes :P
But this is actually not a good reason to avoid this I think :)
Change your comparison to
if (word[i] < 57 && word[i] > 49)
Also, the line is better written asif (word[i] > 49 && word[i] < 57)
It's a more logical order for the comparison pair.
Yes, but isn't the isdigit
function a bit easier to work with?
However if he isn't allowed to use that function it would seem more logical to me if he'd written it like you did :)
>why elcipse doesn't just give an error of "invadid comparision of char with string" or sumthing instead of talking about ints?
Well, actually it does something like that:
C++ forbids comparison between pointer and integer
The pointer where the compiler is complaining about was the string with the double quotes: " "
, the integer where the compiler was complaining about is the character from the string where you want to compare with (In C++ a character can be implicitly converted to an integer) ...
And in case of positive numbers only then consider making it unsigned as well :)
And IMO using LL it's easier to recognize it as a literal (as already mentioned) (there's no doubt possible then) ...
Is your problem solved?
Then please mark the thread as solved, this increases efficiency on the forum :)
I never said this is functional. Just to give him an idea to start.
That's pretty strange, you give him a start (in some 'pseudo code'), but you don't mention the pitfalls :P
I know that it's not working, but if you just turn this code into valid C code then your program will just execute until your pc collapses :P
If you want to give him a good start, then you at least have to give him some good pseudo code as well (if you're giving pseudo code).
Edit:: It might be possible that you did know that pitfall, but probably the OP doesn't know that, and this is his thread right? So I hope it doesn't hurt you that I still mention something like that :P
To start with this routine must do, build on it.
function { sum+=function(string[i+1])*pow(base,i); return sum; }
This is not a functional code, work on it. See the pattern and try it yourself. :)
When do you think that the program stops executing after one call to this 'function' ?
Oh, and heres just a freindly tip. When you declare variables, its preferable to initialize them immediatly, or they will contain 'garbage' that might or might not cause problems. You can initialize them to their null state, like for strings, (" "), for integers (0) and so on :)
Not agreed about the strings...
Consider the following line of code:
string s;
, will it contain garbage?
No.
>but i dont know how to do it
How do you know? Have you already tried it?
If not so, please do so, according to Daniweb's homework policy you should first show your own effort :)
Start with this:
#include <iostream>
using namespace std;
int main()
{
}
You'r entering goals as strings but you aren't comparing the integer values of those strings.
Conclusion:
If you want to store numeric values it's better to use numeric datatypes as well, and not strings, in this case an integer would fit the best, you declare an integer like this:
int mynumber;
And you can use cin to get some data from the user into that variable, like this:
cin >> mynumber;
For storing the number of goals for 'home' and 'guest' you'll have to use integer variables then, so this line:
string home, guest, hnr, bnr;
has to be separated into two lines, one for the integer variables (for storing the number of goals), and one line where the definition of variables 'home' and 'guest' is on (for storing the team's names):
string home, guest;
int hnr, bnr; // declare integer variables
:)
An example of a program which implements the 'Eratosthenes Sieve':
http://www.daniweb.com/code/snippet1205.html :)
And yes, I know, sorry that the code is not fully formatted, that is because I wrote it before I discovered WaltP's excellent code formatting guide :)
And yes siddhant3s, you're probably right that it's better to just make the loop index an unsigned long variable, making it (the value 100001 or something) an unsigned long literal was maybe not the best idea :)
(but it will cause the integer variable each time to be implicitly converted to an unsigned long, so the high order bit will be interpreted in another way, and because the value still fits in that range (if you interpret the high order bit of the integer datatype in another way), it's probably why this nasty trick worked, but now I'm having a doubt whether it's a good practice to do so, personally I don't think so because it's just more error prone (and it wouldn't have worked with larger numbers)...
To the OP: Do what siddhant3s has said: define the variable as an unsigned long :)
>>Adding UL solved the thing and making j long didnt solve it
Don't add UL, Make the j a unsigned long.
I am sensing adding UL is dangerous. But I have no reference right now. I will find and then post it.
j should be a unsigned long
Yeh, I changed it while testing, but I forgot to change it in the OP's code :P
Change your code to this:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int i,j;
long array[100001];
int count=0;
for(i=1;i<100001[B]UL[/B];i++)
{
array[i]=i;
}
for(i=2;i<100001[B]UL[/B];i++)
{
if(array[i]!=0)
{
cout<<""<<array[i]<<endl;
int j=0;
j=i*i;
while(j<100001[B]UL[/B])
{
array[j]=0;
j+=i;
}
}
}
}
:)
I didnt get you. what do you mean by array overrun??
This means that you try to access an element in the array, where you didn't declare memory for...
BTW, you're not using variable count anywhere in your code :)
j=i*i;
while(j<100001)
{
array[j]=0;
j+=i;
}
This will cause an array overrun :)
Edit:: Sorry I replied to quickly :$
2> You better know what you are trying to do over here :
main() char student_info, stname, clas; int rno; {
It's very old C-syntax, not valid anymore in C++ :P
With this low level of code it is probably better posed in the C forum than the c++ forum - I'd give it a try there.
I'll try that! Thanks!
Please do not start a new thread about this topic there, just ask a moderator to move this one to the C forum then :P
You can write your isVowel function in less lines :P
(It can still be written more efficiently, but I want to demonstrate it using a switch case, because the OP used that statement in his code)
bool isVowel (char letter) {
switch (letter) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y': return true;
break;
default: return false;
}
}
Could you post your program's source code as well?
First write it in the way where you think of that it is the most efficient one, then you post your code here, and after you've posted your code, we can analyze it and make suggestions to increase performance :)
>i dunno im sure this output the good result
Did you actually write this?
>i dont understand why it got not the output that i want
Explain us what output you are expecting, then it's easier for us to help you fixing the problem :) [B]return getchar();[/B]
, it's not good to write it like that, it's better to split it into two separate instructions like:
getchar();
return 0;
Probably nothing wrong with Turbo Vision, but that's not the issue here. TVision is a library, not a compiler.
Yea, but I was just giving another suggestion for: 'Graphical Dos Interface' :)
(And of course I did know that Turbo Vision isn't a compiler, but I agree with you that you still mention it because it could maybe confuse some other people)
As the OP is talking about DOS I would like to suggest another compiler (especially ported to DOS) as well...DJGPP, it's just such a fantastic compiler, and the utilities it comes with are also very likeable...
Edit:: And *forgot to mention* ... Turbo Vision works on it as well :P
>I would be very happy if u send me code.
Ever considered reading this?
>sir how can i add two fraction nos. to get again fraction
eg.
a/b+c/d=(ad+bc)/bd
I even don't understand a single word of what you're saying :P
I just see that I posted using C++ code tags instead of C code tags, however it doesn't affect your code or something, I just wanted to let you know :)
What about Turbo Vision?
(no not the one originally developed by Borland, but a completely compatible one which is more secure and which supports more compilers/platforms)
:)
>i need some help in understanding pointers
I assume you are talking about the following?
int string_len(char *s) {
char *p;
p = s;
while (*s != '\0') { // loop until the null-terminator (1)
s++;
}
return s-p; // return the string's length (2)
}
(1) When this loop has finished, s points to the null terminator of the string.
(2) return s-p;
: In the beginning of your function you let point p to 'first' character of the string (actually it is more correct to say: you let pointer p point to pointer s, this is the point from where you start counting)
So applying the rules of pointer arithmetic your function returns the length of the string :)
>Also the use of pointers with structures and passing on arguments onto functions confusing me.
For a complete reference on pointers I would recommend you to read this :)
(your requested examples are also covered here)
Then stop replying posts you have no intention of of adding constructive responses to!
Adatapost, you do not have to use a jephthah-approved compiler.
Agreed, but if you use such an old dinosaur compiler then you're nearly always forced to write non-jephthah approved code :P
In my opinion if you want to control the much loved powers of the C++ language, you should use a standard compiler, not such an old one, why else was C++ standardized ?? (<- yes Narue, I know you're going to give feedback on this :P)
>It's a legitimate question. Are you really trying to accomplish something by flaming the guy, or just being an ass?
I didn't want to blame anyone, Narue, but didn't you once write the following?
Search for your answer first!
If you're having a problem, chances are good that someone else has had the same problem. Please search the forum for existing answers before starting a new thread. Nothing is more irritating to a long time member than to answer the same question for the umpteenth time because someone didn't use the forum's search feature.
Don't resurrect old threads!
Inevitably, if you use the search feature you'll stumble upon an interesting thread that you think you can add to. Resist the temptation to post until you've looked at the post date for the most recent post in that thread. It's possible that the thread is years old and you have no business bumping it to the top of the list. A thread is considered old after two months.
Let dead threads lie, don't post in them!
just look at the constructor, and understand how nice person i am.
look at the values that i pass..
Come on mate, every post you make gets creepier!
Serkan, what a f****** idiot are you?? :angry:
You just can't have a single reason to give WH bad rep for such a useful post!
And don't say that it was by accident, you can only give good rep by accident, not bad rep!
Sorry for my behaviour :$ ...
>I know im just confused but could you give a little light on the part regarding some byte can be 16-bit or 32-bit?
International character sets just have too much possible combinations, but all those possible combinations don't fit in 1 byte (= 8 bits), so multiple bytes have to be used to make it possible to store all those different combinations (for example in Unicode)
1 byte is enough for storing 256 possible combinations (2^8), applied to a character set this means that it can hold 256 different characters (e.g: the ASCII character set)
But Unicode is an international character set, defining much more characters than 256...
Currently C++ doesn't support all Unicode characters, only the most common one...
:)
>And I've read that you should use sizeof to determine the size of a byte?
No, you read it wrong, a byte is a general term for any valid combination of eight bits (and a bit is a one or a zero)
('To determine the size of a byte' ?? A byte is just always eight bits, you cannot hesitate here)
An example of a byte would be: 00001010 (which is ten in decimal) sizeof
is used to get the number of bytes occupied by any valid datatype (including structs, your own classes), for example [B]sizeof(int)[/B]
will return the number of bytes an integer variable will take up in your computer's memory (on most implementations (this means not on all) this is 4, but as you already said, it can differ from the implementation)
>I've read that the bits in a byte (in c++) are implementation or system dependent. What does that mean?
The C++ standard does not define the exact number of bytes a datatype has to consist of, however it just simply states things like: an integer variable will always have the natural size suggested by the architecture of the execution environment (32bits = 4 bytes for a 32bit processor, 16bits = 2 bytes for a 16bit processor, etc...)
In other words: The C++ standard defines minimum requirements for them :) sizeof
is an operator which is frequently used to ensure portability: at compile time the sizeof([B]<something>[/B])
'instruction' will be replaced by …
>Can you please explain a little how you derived the formula c = c * (y - x) / (x + 1); ....just to get the idea of what steps to do in order to reach this formula
Dude, are we going the reinvent the whole history of maths?
No!
BTW, Do you think that reviving a three year old thread is such a good idea?
I don't think so.
(Just check out this and if you've any more questions, then you should start a new thread about it)
>pdcurses works well with code::blocks but is there any way to use pdcurses with visual Studio, plz help.
Yes, it's possible, look at the README file :)