473,387 Members | 1,742 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,387 software developers and data experts.

Frustrated. How do I use void display(char * str) in a header file?

5
Ugh.

I am taking a c++ foundations course, and all was well until the moment came where I had to break my prototypes out into .h files. Until this point, all my prototyping has been done with ints, doubles, etc. - char seems to be different and vexing.

test.h

void display(char* str)
---------------------------------------------
int main()
{
#include "test.h"
char str[] = "hello";
display();
}

At this point I get an error on compile and nothing I have done, or none of the reference material I have at my disposal is fixing the prolem.

HELP!
May 19 '07 #1
9 2560
ilikepython
844 Expert 512MB
Ugh.

I am taking a c++ foundations course, and all was well until the moment came where I had to break my prototypes out into .h files. Until this point, all my prototyping has been done with ints, doubles, etc. - char seems to be different and vexing.

test.h

void display(char* str)
---------------------------------------------
int main()
{
#include "test.h"
char str[] = "hello";
display();
}

At this point I get an error on compile and nothing I have done, or none of the reference material I have at my disposal is fixing the prolem.

HELP!
Your include commands should be at the top of the file and don't forget to add a semicolon after your prototype. Also, your display function takes a char pointer but you aren't passing it any. Make sure that you actually have the body for the display function.
Expand|Select|Wrap|Line Numbers
  1. #include "test.h"
  2.  
  3. int main()
  4. {
  5.     char str[] = "Hello";
  6.     display(str);
  7.     return 0;
  8. }
  9.  
Don't get confused by characters, they are just another data type like ints or doubles.
May 19 '07 #2
bent
5
Thanks! - that was the vital piece -

I am now getting a return value of ''__Z5helloPc' - which is not right.

The whole program looks like:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. #include <limits>
  4. int main()
  5. {
  6. #include "testing.h"
  7.     char str[] = "hello";
  8.     hello(str );
  9.  
  10.  
  11.     cout << str[0]
  12.         << str[1]
  13.         << str[2]
  14.         << str[3]
  15.         << str[4]
  16.         << endl;
  17.  
  18.  
  19.     cout << str[4]
  20.         << str[3]
  21.         << str[2]
  22.         << str[1]
  23.         << str[0]
  24.         << endl;
  25.  
  26.     return 0;
  27. }
  28.  
I am not sure where the garbage is coming from.

it should return "hello
olleh"

Which it does when the function is in main().
May 19 '07 #3
Savage
1,764 Expert 1GB
Thanks! - that was the vital piece -

I am now getting a return value of ''__Z5helloPc' - which is not right.

The whole program looks like:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. #include <limits>
  4. int main()
  5. {
  6. #include "testing.h"
  7.     char str[] = "hello";
  8.     hello(str );
  9.  
  10.  
  11.     cout << str[0]
  12.         << str[1]
  13.         << str[2]
  14.         << str[3]
  15.         << str[4]
  16.         << endl;
  17.  
  18.  
  19.     cout << str[4]
  20.         << str[3]
  21.         << str[2]
  22.         << str[1]
  23.         << str[0]
  24.         << endl;
  25.  
  26.     return 0;
  27. }
  28.  
I am not sure where the garbage is coming from.

it should return "hello
olleh"

Which it does when the function is in main().
It's probably coming from function.

And what do u mean by:

"Which it does when the function is in main()."?

Ur hello function is currently in main.

Please can u give us more info..

Savage
May 19 '07 #4
bent
5
It's probably coming from function.

And what do u mean by:

"Which it does when the function is in main()."?

Ur hello function is currently in main.

Please can u give us more info..

Savage
my testing.h header file contains:

Expand|Select|Wrap|Line Numbers
  1. void hello(char* str);
  2.  
if I put

Expand|Select|Wrap|Line Numbers
  1. char * str = new char[5];
  2. str = "hello";
  3.  
in the main body of my program, it returns my expected results. It is when I include the header file, I get the garbage return value.
May 19 '07 #5
Savage
1,764 Expert 1GB
my testing.h header file contains:

Expand|Select|Wrap|Line Numbers
  1. void hello(char* str);
  2.  
if I put

Expand|Select|Wrap|Line Numbers
  1. char * str = new char[5];
  2. str = "hello";
  3.  
in the main body of my program, it returns my expected results. It is when I include the header file, I get the garbage return value.
Can we see function hello definition?

Savage
May 19 '07 #6
bent
5
Can we see function hello definition?

Savage
Savage,

This is where I am getting ignorant. Function hello only exists as a prototype in the header file. everything else I have posted is the entirety of the program.
I am a rank beginner at OOP, so please forgive me if I am missing something or mis-understanding something.

The goal of the program is to use pointer arithmatic to display the string "hello" forwards and bachwards . As far as coding it without using the header file, I am successful.
So far, I have been unable to include the header in any meaningful way. Hence my frustration.
May 19 '07 #7
Savage
1,764 Expert 1GB
Savage,

This is where I am getting ignorant. Function hello only exists as a prototype in the header file. everything else I have posted is the entirety of the program.
I am a rank beginner at OOP, so please forgive me if I am missing something or mis-understanding something.

The goal of the program is to use pointer arithmatic to display the string "hello" forwards and bachwards . As far as coding it without using the header file, I am successful.
So far, I have been unable to include the header in any meaningful way. Hence my frustration.
I allready know all that and this is probably ur problem:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. #include "testing.h"
  4.     char str[] = "hello";
  5.     hello(str );
As ilikepython said ur include commands should be at the top of the code.

(Sorry,didn't noticed it when I was looking before few minutes)

Savage
May 19 '07 #8
bent
5
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. #include "testing.h"
  4.     char str[] = "hello";
  5.     hello(str );
  6.  
The problem, it appeared was on the final line above, with referenced the prototype, but didn't have any play with the variables. Removing that line fixed the output.

Thanks!
May 20 '07 #9
Savage
1,764 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. #include "testing.h"
  4.     char str[] = "hello";
  5.     hello(str );
  6.  
The problem, it appeared was on the final line above, with referenced the prototype, but didn't have any play with the variables. Removing that line fixed the output.

Thanks!
Allways a pleasure..

Savage
May 20 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: dave | last post by:
Hi I m facing strange problem... I have one field char type data length 1.. It has data either 1 or 2 in all the field tht I have checked through enterprise manager. I'm running query "select...
0
by: Keith Dick | last post by:
I'm trying to use the free C/C++ compiler for .NET. When I debug a program with CORDBG and use the p command to display a char array, it displays the data as numbers, not as characters. How can I...
1
by: Steffen Fiksdal | last post by:
I have a function that base64 decodes some data. The incoming data is received as "const char*" (BASE64 characters are always safe ASCII characters, meaning they will always fit in a signed char...
5
by: Martin Moser | last post by:
Does somebody knows, if theres a way to display any file (tiff, doc, pdf, .....) "inline" on an asp.net site? What I want to do is, to display a file, which is stored in the DB, and some...
5
by: Geoff Pennington | last post by:
My VB.Net app reads an Excel file, processes it one row at a time, and when processing is complete writes the row to a database. The typical file will have several thousand rows and may take a...
1
by: Tim | last post by:
Can anyone tell me how can i use ASP to display a file in the web browser? the file is located on the server in C:\temp directory. It can be any file. Is there any way that i can do this? Thank...
1
by: shapper | last post by:
Hello, I am trying to convert an Asp.Net XML sitemap file in a Google XMl sitemap file using a XSL file using an HttpHandler. Everything seems well in my code but I am getting an error: XML...
15
by: Matt | last post by:
Is there a way to display the file selection window for a file input field via JavaScript? My goal is to emulate the behavior seen in Yahoo! Mail BETA. When adding an attachment, it displays a...
2
by: BASSPU03 | last post by:
I used the Common Dialog API to store file paths on my form's underlying table. These paths are displayed in a textbox that I can click to open the selected file. Having stored the file paths in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.