I need help as i m new in C++

J

James Kanze

There is no way to complete your assignment. 'z' is not among the
fist five letters in the english alpabet.
And, no, you don't have to ascii. 'a' + 1 == 'b' regardless of the
representation of the 'alpabet'.

Not at all. The standard guarantees that the decimal digits are
in successive ascending order, but that is all. Anything else
depends on the implementation (and in EBCDIC, the letters aren't
all contiguous---'i' + 1 is '«').

Of course, that still doesn't mean that you need ASCII. What
you need is a table of the legal letters, and the integers 0 to
n-1; you generate using the integers, and use the table to
output. That way you're flexible, and you can even make it work
for non-English alphabets.
 
D

Default User

Victor said:
There is no way to complete your assignment. 'z' is not among the
fist five letters in the english alpabet.

How do you know? He didn't define what the "fist five" are. In fact,
from the example we can determine that the fist five must be: a, b, c,
d, z. I assume these are letters that pack a wallop.



Brian
 
V

Victor Bazarov

Default said:
How do you know? He didn't define what the "fist five" are. In fact,
from the example we can determine that the fist five must be: a, b, c,
d, z. I assume these are letters that pack a wallop.

Where do you get this? My definition of "english alpabet" says that
the fist five aren't five at all. They are actually seventeen, and
'z' is not one of them. It includes the letters "baa" and "maa", the
symbols for scratching one's forehead and for picking one's nose.
Also among them the symbols for a typo, a sigh, and a smiley.

V
 
D

Default User

Victor said:
Default User wrote:

Where do you get this? My definition of "english alpabet" says that
the fist five aren't five at all. They are actually seventeen, and
'z' is not one of them. It includes the letters "baa" and "maa", the
symbols for scratching one's forehead and for picking one's nose.
Also among them the symbols for a typo, a sigh, and a smiley.

That's the "left fist five" (everywhere except Boca Raton, Florida).
The default is the "right fist five".




Brian
 
X

Xohaib

Calculate the first X prime numbers, where X is input from the user.



#include <iostream.h>
#include <stdlib.h>
void main()
{
a:
int num,prime=1;
char restart;
cout<<"Enter the number you want to check prime or not ";
cin>>num;
if (num==1)
{
cout<<"Not prime\n";
}
for (int i=2; i<num; i++){
if (num%i==0)
{
cout<<"Number is not prime\n";
break;
}
}
if (num==i)
{
cout<<"Number is prime\n";
}
cout<<"[If you want to restart the program press y else press n; (Y/
N)] ";
cin>>restart;
if (restart=='y' || restart=='Y')
{
system("cls");
goto a;
}
}


what about it dude ? Is it right ?
 
X

Xohaib

heres one.

make a program that will brute force every letter comination of the
fist five letters in the english alpabet.

ex.
aaaaa
aaaab
aaaac
aaaad

ect...

utill you get to

zzzzz

i will give you a hint you have to ascii

Hey dude thanxx for such a program i think u mean top say this



{
for (char i=97; i<=122; i++){
for (char i1=97; i1<=122; i1++){
for (char i2=97; i2<=122; i2++){
for (char i3=97; i3<=122; i3++){
for (char i4=97; i4<=122; i4++){
cout<<i<<i1<<i2<<i3<<i4<<endl;
}
}
}
}
}
}


am i right na ?

I made it myself so keep me helping to improve my C++

Regards

Xoahib
 
I

Ioannis Gyftos

All right, I'll try to post my comments :) (ignoring cin exceptions, 0
as input etc). Correct me if I'm wrong anywhere.
#include <iostream.h>
#include <stdlib.h>

First of, in C++ programs you should prefer the non-h versions of the
headers, like <iostream> and <stdlib>. The versions you used are
targeted at C programs, are deprecated, and are maintained in C++ only
for compatibility, so avoid it. Some compilers warn about this.
void main()

In C++, use int main(). Void might be accepted on some compilers, but
i think it officially is undefined behavior.
cout<<"Enter the number you want to check prime or not ";
cin>>num;

If compiled like that, cout/cin are not declared. These are included
in the standard library, so you should instead either use "std::cout"
and "std::cin" (very recommended) or after your #includes, add the
line "using namespace std;" which has a similar effect.
for (int i=2; i<num; i++){
...
}
if (num==i)

Here, the variable "i" is declared inside the for loop. This means it
is a valid identifier only until the } where the scope of the for
ends. When you try to use it in the if, which is outside its scope,
the compiler should complain that i is undeclared. For example, in my
gcc:

..../src/primetest.cpp:43: error: name lookup of 'i' changed for new
ISO 'for' scoping
..../src/primetest.cpp:36: error: using obsolete binding at 'i'

If your compiler accepts this code, you should probably try to find a
newer version. To work around this, the easiest way is to define i
outside the for loop, like:

int i=2;
for (; i < num; i++){/*...*/}
if (num==i){/*...*/}
system("cls");

I hope you are aware that this is very platform dependant, and not
standard C++. In my machine, when your program reaches this point i
get:

sh: cls: not found

Which I assume is not what you meant :)

"goto" is generally viewed as bad practice. C++ has better and more
easily comprehended mechanisms to achieve similar effects, so avoid it
unless you have a good reason not to. For example, you could use a
while() construct that exits when a certain flag becomes true. A
simplified similar approach:

/* ... */
bool flag=false;
while(flag==false){

/* read user input */
/* check if it's a prime and print the result */

cin >> restart;
if (restart=='y' || restart=='Y')
{
flag = true;
}

/* do anything else - like clearing the screen */
}

Actually, though, what this program does is not what I meant :) You
read a number from the user and calculate whether it is prime or not,
and repeat. What I meant, was that the user would type a number (e.g.
6) and the program would calculate the first 6 prime numbers (e.g. 2,
3, 5, 7, 11, 13).

One way to do this, similar to this program, would be to have a
function that checks if any number is prime. Then have a variable that
stores the number of prime numbers you have found so far, and a while
loop that each time calculates the next prime number and ends when you
find all of them that the user asked for.

A better way, though, would be to have an array (see std::vector) of
consecutive ints: 2,3,4,5,6,... (1 doesn't count in this case). Mark
them all as possible-primes. Then, inside a loop, get the smallest
possible-prime value, and mark all its integer multiplicands(sp?) as
non-prime. When you loop as many times as the user typed (say X), then
the X first possible-primes is what you are looking for. For ease,
have the size of the array of consecutive ints be X*X initially.

(or use an std::list and remove the non-primes maybe)
 
D

Default User

Ioannis said:
All right, I'll try to post my comments :) (ignoring cin exceptions, 0
as input etc). Correct me if I'm wrong anywhere.


First of, in C++ programs you should prefer the non-h versions of the
headers, like <iostream> and <stdlib>. The versions you used are
targeted at C programs, are deprecated, and are maintained in C++ only
for compatibility, so avoid it. Some compilers warn about this.

Wow, a lot of bad information. <iostream.h> is not a C++ header,
deprecated or not. It's certainly not a C header. <stdlib.h> is a
standard header in both, although deprecated in C++. There's no header
<stdlib> in C++, the correct one is <cstdlib>.





Brian
 
I

Ioannis Gyftos

<iostream.h> is not a C++ header, deprecated or not.

I think I disagree here. Quick google:
http://www.devx.com/tips/Tip/14447
<stdlib.h> is a standard header in both, although deprecated in C++.
There's no header <stdlib> in C++, the correct one is <cstdlib>.

Yes, you are correct here.

A simple "Hello World" program automagically generated by kdevelop
3.4.0 has:

#include <iostream>
#include <cstdlib>
 
J

James Kanze

Ioannis Gyftos wrote:
Wow, a lot of bad information. <iostream.h> is not a C++ header,
deprecated or not.

That's not true. It's not a "ISO standard" C++ header, but it
was part of pre-ISO C++ (which isn't all that long ago).
 
D

Default User

Ioannis said:
I think I disagree here. Quick google:
http://www.devx.com/tips/Tip/14447

<iostream.h> and its friends never made it into the Standard. They are
simply non-standard and may not be supported by implementations. The C
headers are in the Standard, but are deprecated (or whatever specific
term is used).

The web page you cite obviously doesn't know the meaning of the term
"deprecate" as it applies to standardized languages.




Brian
 
D

Default User

James said:
That's not true. It's not a "ISO standard" C++ header, but it
was part of pre-ISO C++ (which isn't all that long ago).

So? The language discussed here is ISO standard C++. It's certainly not
the case that it's "deprecated" as that would require a standard to
deprecate it.

<iostream.h> is non-standard and programs that use it may be rejected
by conforming compilers. That not currently the situation with
<stdio.h>.




Brian
 
J

James Kanze

So? The language discussed here is ISO standard C++.

Since when? The name of the group is not comp.lang.iso-c++.
The group existed long before there was an ISO standard for C++,
and the ISO standard is not mentionned in the charter.
It's certainly not the case that it's "deprecated" as that
would require a standard to deprecate it.

Deprecated is a normal English word, with a meaning outside of
the standard.
<iostream.h> is non-standard and programs that use it may be
rejected by conforming compilers.

As a quality of implementation issue, however, it won't be.
Quality implementations are concerned about existing code. They
may output a warning, however, indicating that using the header
is an anachronism. (G++, for example, does, although the
warning can easily be suppressed. By means of the option
-Wno-deprecated.)

I might also remind you that conforming compilers are very, very
rare. About the only one I have access to is Comeau, and I'm
not allowed to install it on my machines at work (where we use
Sun CC, g++ and VC++---none of which are conforming). In
practice, what we discuss in this group is real C++, not some
pie in the sky theoretical language.
That not currently the situation with <stdio.h>.

A compiler could also output a warning about <stdio.h>, if it
wanted to. At least some posters here would probably approve.
 
V

Virtual_X

Hi! every one i am new in c, c++ i have just started it... i mean to
say that i have jsut learnt to include iostream and how to use if
condition, and cin, and cout but with the help of different sites i
have leartn how to use for loop.... but i am not perfect....

The thing over here i want to say is that give me the assignment to
make different programms like ask me to make a program that takes a
number as input from user and make table of it..... Is it interesting
game na...... yes.

So what are you waiting for? ask me the programs which are made bye
using if and OR gate AND gate and cin,cout as i am working in VC 6.0.
So are u agree.........

LETS play it ...............

i think it will be very useful to you to read "Thinking in C++" book ,
it's free and will learn you C++ with a big amount of exercises "Try
it,it's really very good"

you can find it at
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html#DownloadingTheBook

after that you can advance more in c++ by this link
http://www.doc.ic.ac.uk/lab/cplus/c++.rules/
 
I

Ioannis Gyftos

Meh, this is getting quite a bit off the point, eh? I don't see a
reason for this.

Bottom line, avoid that header, do we agree?
 
D

Default User

James said:
Since when? The name of the group is not comp.lang.iso-c++.
The group existed long before there was an ISO standard for C++,
and the ISO standard is not mentionned in the charter.

So? Things change. While older versions can be discussed in an
historical context, non-standard constructs are off-topic for new code
development.
Deprecated is a normal English word, with a meaning outside of
the standard.

That doesn't matter. As this is a technical group, the technical term
in context is what matters.
As a quality of implementation issue, however, it won't be.

I believe there are compilers now that do so, although I can't cite any
specifics.
A compiler could also output a warning about <stdio.h>, if it
wanted to. At least some posters here would probably approve.

A compiler could complain about your bracing style too. That doesn't
change anything. What I said was 100% accurate, and you just want to be
contentious. As such, I won't bother reading anything further you have
to say in this thread.



Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top