473,386 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

new at c++ have some questions

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
12 1428
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
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

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

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
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
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
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
"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
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
IR
Kaz Kylheku wrote:
[...]
The designers of ANSI Common Lisp disagree:
[...]

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ross Micheals | last post by:
All I have some general .NET questions that I'm looking for some help with. Some of these questions (like the first) are ones that I've seen various conflicting information on, or questions that...
12
by: Vibhajha | last post by:
Hi friends, My sister is in great problem , she has this exam of C++ and she is stuck up with some questions, if friends like this group provides some help to her, she will be very grateful....
1
by: Tony Johansson | last post by:
Hello Experts! I have some questions about inheritance that I want to have an answer to. It says "Abstract superclasses define a behavioral pattern without specifying the implementation" I...
162
by: techievasant | last post by:
hello everyone, Iam vasant from India.. I have a test+interview on C /C++ in the coming month so plz help me by giving some resources of FAQS, interview questions, tracky questions, multiple...
50
by: Jatinder | last post by:
I 'm a professional looking for the job.In interview these questions were asked with some others which I answered.But some of them left unanswered.Plz help. Here are some questions on C/C++, OS...
24
by: Kevin | last post by:
Hey guys. I'm looking to get together some VB programmers on Yahoo messenger. I sit at a computer and program all day. I have about 3 or 4 people already, but it would be really cool to have a...
7
by: changs | last post by:
Hi, all I have a asm code, I suspect it sort of socket programming. Can anyone here give some instructions on how to determine the function or give the psudo-code in C? Thanks in advance! ...
3
by: iKiLL | last post by:
Hi all I am building an Windows Mobile 5 forms control in C#, for a Windows Mobile 5 application. I am using CF2.0 and SQL Mobile 2005. The control is a Questions and answer control.
4
by: Mr. X. | last post by:
Hello. I need some help, please. What can an employee ask (technical questions), if I am interviewed of Dot-net developer (also VB.NET and C#). (What are the most general questions, that I may...
30
by: GeorgeRXZ | last post by:
Hi Friends, I have some questions related to C Language. 1What is the difference between the standard C language and Non standard C language ? 2which is better C Lanugage, C under Linux/...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.