By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,128 Members | 1,034 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,128 IT Pros & Developers. It's quick & easy.

new at c++ have some questions

P: n/a
Hi,

I am trying to learn C++ and have some questions. I get a
"Segmentation fault", whatever that is, when I try this simple program.

Basically the program is suppose to print "five". I have been reading
on printf and I think I found a bug with the %s symbol. it says %s is
suppose to print the string but it does not work!

Thanks be to you!
#include <stdio.h>

#define is =
#define start main()

start
{
int x is 5;

printf("the value of x is %s\n", x);
}

Dec 19 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
IR
wintaki wrote:
Hi,

I am trying to learn C++ and have some questions. I get a
"Segmentation fault", whatever that is, when I try this simple
program.

Basically the program is suppose to print "five". I have been
reading on printf and I think I found a bug with the %s symbol.
it says %s is suppose to print the string but it does not work!

Thanks be to you!
#include <stdio.h>

#define is =
#define start main()
If you want to write C++, first ditch that kind of macros. They
don't add any clarity to the language, they just make it harder to
understand.
start
Macros resolved, main() is not C++. int main() is.
{
int x is 5;

printf("the value of x is %s\n", x);
}
Now, of course you get a segfault. %s is supposed to print a
*string*.
What you provide it is an *int*. Read printf's documentation
(completely!).
Cheers,
--
IR
Dec 19 '06 #2

P: n/a
wintaki wrote:
Hi,

I am trying to learn C++ and have some questions. I get a
"Segmentation fault", whatever that is, when I try this simple program.
It means that your program attempts to access memory that doesn't belong to
it.
Basically the program is suppose to print "five". I have been reading
on printf and I think I found a bug with the %s symbol. it says %s is
suppose to print the string but it does not work!
It's a bug in your program. %s is indeed used to print strings. The problem
is that you don't provide a string, but an int. Since printf isn't type
safe, it won't notice the problem. Instead, it just tries to read a string
from where actually an int is stored.
#include <stdio.h>

#define is =
#define start main()
What are those supposed to be good for, other than obfuscate the code?
start
{
int x is 5;

printf("the value of x is %s\n", x);
}
Dec 19 '06 #3

P: n/a

Rolf Magnus wrote:
wintaki wrote:
Hi,

I am trying to learn C++ and have some questions. I get a
"Segmentation fault", whatever that is, when I try this simple program.

It means that your program attempts to access memory that doesn't belong to
it.
this is certainly not what i want to do. i do not wish to access
someone else's memory. my goal is to start with 5 and have it print
the string version. as I understand it a string is a text value, and a
"5" is an int value.
>
Basically the program is suppose to print "five". I have been reading
on printf and I think I found a bug with the %s symbol. it says %s is
suppose to print the string but it does not work!

It's a bug in your program. %s is indeed used to print strings. The problem
is that you don't provide a string, but an int. Since printf isn't type
safe, it won't notice the problem. Instead, it just tries to read a string
from where actually an int is stored.
#include <stdio.h>

#define is =
#define start main()

What are those supposed to be good for, other than obfuscate the code?
ok I will stop with those, I was just testing it out since I am new to
#define and thought it was nice to be able to "customize" C++ to my
liking. but I see your point.

But then the new program is
int main()
{
int x = 5;

printf("the value is %s\n", x);
}
but i still have the same problem. are saying is that I can not use %s
with an int value? but then how do I get a text value? I wanted to
write a simple program that took the x value and printed it in words.
i thought %s converted the value to text?

thanks

Dec 19 '06 #4

P: n/a
On 19 Dec 2006 11:10:05 -0800 in comp.lang.c++, "wintaki"
<wi*****@hotmail.comwrote,
printf("the value is %s\n", x);
>but i still have the same problem. are saying is that I can not use %s
with an int value?
Yes. Use %d instead, that requests printf() to convert the int to text
for output.

Or better, use real C++
std::cout << x;

Since the stream operator<<() know its argument type, unlike printf,
it can do the right thing without further help.

Dec 19 '06 #5

P: n/a

wintaki wrote:
Rolf Magnus wrote:
wintaki wrote:
Hi,
>
I am trying to learn C++ and have some questions. I get a
"Segmentation fault", whatever that is, when I try this simple program.
It means that your program attempts to access memory that doesn't belong to
it.

this is certainly not what i want to do. i do not wish to access
someone else's memory. my goal is to start with 5 and have it print
the string version. as I understand it a string is a text value, and a
"5" is an int value.
Basically the program is suppose to print "five". I have been reading
on printf and I think I found a bug with the %s symbol. it says %s is
suppose to print the string but it does not work!
It's a bug in your program. %s is indeed used to print strings. The problem
is that you don't provide a string, but an int. Since printf isn't type
safe, it won't notice the problem. Instead, it just tries to read a string
from where actually an int is stored.
#include <stdio.h>
>
#define is =
#define start main()
What are those supposed to be good for, other than obfuscate the code?

ok I will stop with those, I was just testing it out since I am new to
#define and thought it was nice to be able to "customize" C++ to my
liking. but I see your point.

But then the new program is
int main()
{
int x = 5;

printf("the value is %s\n", x);
}
but i still have the same problem. are saying is that I can not use %s
with an int value? but then how do I get a text value? I wanted to
write a simple program that took the x value and printed it in words.
i thought %s converted the value to text?
You're using the 'c' way of doing things. The c++ way to do that would
use std::cout

#include <iostream>
int main() {
int x = 5;
std::cout << "The value is " << x;
}

If you still wanted to use printf, you need to use a %i or %d to
display an int. Do a google search on 'printf format identifiers' for
more information about that.

Dec 19 '06 #6

P: n/a
wintaki wrote:
ok I will stop with those, I was just testing it out since I am new to
#define and thought it was nice to be able to "customize" C++ to my
liking.
``I can't write a program that prints a number, yet I want to customize
C++ to my liking''.

LOL

Dec 19 '06 #7

P: n/a
On Dec 19, 8:10 pm, "wintaki" <wint...@hotmail.comwrote:
int main()
{
int x = 5;

printf("the value is %s\n", x);

}

but i still have the same problem. are saying is that I can not use %s
with an int value? but then how do I get a text value? I wanted to
write a simple program that took the x value and printed it in words.
i thought %s converted the value to text?
There's no function or such that will magically convert the integer 5
to the string "five", after all, who said that the program was written
in English, I'm Swedish so I'd want the program to print "fem". If you
really want the program to print the name of a number you'll have to do
it yourself, a start would be something like:

switch (a%10)
{
case 0:
std::cout << "zero";
break;
case 1:
std::cout << "one";
break;
case 2:
std::cout << "two";
break;
}

--
Erik Wikström

Dec 20 '06 #8

P: n/a
wintaki wrote:
Hi,

I am trying to learn C++ and have some questions. I get a
"Segmentation fault", whatever that is, when I try this simple program.

Basically the program is suppose to print "five". I have been reading
on printf and I think I found a bug with the %s symbol. it says %s is
suppose to print the string but it does not work!

Thanks be to you!
#include <stdio.h>

#define is =
#define start main()

start
{
int x is 5;

printf("the value of x is %s\n", x);
}
Try this code

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

int main()
{
int x=5;
cout<<x;

return 0;
}
--
WickedInnovation.com
http://www.wickedinnovations.com/
Best website design company, Offers professional web design and
development service, try our Consultation from our Experts for FREE!!!

Dec 20 '06 #9

P: n/a
"Erik Wikström" <er****@student.chalmers.sewrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...
On Dec 19, 8:10 pm, "wintaki" <wint...@hotmail.comwrote:
int main()
{
int x = 5;

printf("the value is %s\n", x);

}

but i still have the same problem. are saying is that I can not use %s
with an int value? but then how do I get a text value? I wanted to
write a simple program that took the x value and printed it in words.
i thought %s converted the value to text?
There's no function or such that will magically convert the integer 5
to the string "five", after all, who said that the program was written
in English, I'm Swedish so I'd want the program to print "fem". If you
really want the program to print the name of a number you'll have to do
it yourself, a start would be something like:

switch (a%10)
{
case 0:
std::cout << "zero";
break;
case 1:
std::cout << "one";
break;
case 2:
std::cout << "two";
break;
}

-------------------------------------

Personally, I would probalby write it like this.

const char* const Numbers[] = { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten" };

if ( x >= 0 && x < 10 )
std::cout << Numbers[x] << "\n";

std::cout is the c++ way of printing to standard out. If you insist on
using printf (I don't know why you would) it would become:

if ( x >= 0 && x < 10 )
printf( "%s\n", Numbers[x] );

The %s in the printf string says to print a c-style string, which means you
need to give it a char* (character pointer).

printf does not check the types of the parameters passed, and it will
blindly attempt to do what you tell it. In your case, print an interger as
a c-string, treating the int as a char pointer, which pointed somewhere
(most like into memory location 0x0000005 but there is no guarantee) which
your program doesn't "own", hence you got the segmentation fault. This is
why std::cout is safer.
Dec 20 '06 #10

P: n/a
Erik Wikström wrote:
On Dec 19, 8:10 pm, "wintaki" <wint...@hotmail.comwrote:
int main()
{
int x = 5;

printf("the value is %s\n", x);

}

but i still have the same problem. are saying is that I can not use %s
with an int value? but then how do I get a text value? I wanted to
write a simple program that took the x value and printed it in words.
i thought %s converted the value to text?

There's no function or such that will magically convert the integer 5
to the string "five", after all, who said that the program was written
in English, I'm Swedish so I'd want the program to print "fem".
The designers of ANSI Common Lisp disagree:

;; English cardinal
(format t "~r~%" 5)
five

;; English ordinal
(format t "~:r~%" 5)
fifth

;; Roman
(format t "~@r~%" 5)
V

:)

Dec 21 '06 #12

P: n/a
IR
Kaz Kylheku wrote:
[...]
The designers of ANSI Common Lisp disagree:
[...]

Now, who's the troll?
Cheers,
--
IR
Dec 22 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.