new at c++ have some questions

W

wintaki

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);
}
 
R

Rolf Magnus

wintaki said:
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?
 
I

IR

wintaki said:
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.

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,
 
W

wintaki

Rolf said:
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.
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.


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
 
D

David Harmon

On 19 Dec 2006 11:10:05 -0800 in comp.lang.c++, "wintaki"
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.
 
J

jjds101

wintaki said:
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.


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.
 
K

Kaz Kylheku

wintaki said:
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
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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;
}
 
J

Jenna0109

wintaki said:
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;
}
 
J

Jim Langston

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.
 
K

Kaz Kylheku

Erik said:
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

:)
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top