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

why do i get garbage output?

using Dev C++4
user can enter names of cities and temps,
but when it output city names i get garbage but
temps are fine.
struct Cities {
char city[];
int temp;
};
void input_names(Cities info[], int numb_cities) {

for(int t=0;t<numb_cities;t++) {
cout<<"\n\n";
cout<<"please enter name of city#"<<t+1<<": ";cin>>info[t].city;
cout<<"\nplease enter temp of city "
<<info[t].city<<": ";
cin>>info[t].temp;
}

cout<<"\n\n";
for(int t=0;t<numb_cities;t++) {
cout<<info[t].city<<" "<<info[t].temp<<"\n";
}

}//close input_names;

void main() {
int n,
numb_cities=10; //number of cities

void input_names(Cities[], int);

Cities info[numb_cities];
input_names(info, numb_cities);
}

--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------
Jul 19 '05 #1
10 2260
Developwebsites wrote:
using Dev C++4
user can enter names of cities and temps,
but when it output city names i get garbage but
temps are fine.
struct Cities {
char city[];
use
string city;

instead
int temp;
};

The problem is that cin >> char [] does not allocate storage, you need
to do that. std::string does all the magic of allocating storage and
cleaning it up.

Jul 19 '05 #2

"Developwebsites" <de*************@aol.combatSPAM> wrote in message news:20***************************@mb-m06.aol.com...
struct Cities {
char city[];
int temp;
};
This shouldn't even compile. The array city needs a definite size. Frankly,
I suspect you're new to C++. Don't beat char arrays to death. Use the string
class.

cin>>info[t].temp;
You don't test if these finputs ail.

void main() {
main must return int.
void input_names(Cities[], int);
You don't need to repeat the declaration here.
Cities info[numb_cities];


This is also not legal. Array declarations in C++ need sizes that are constant
expressions (I bet you're using an anceint G++).

I'd recommend you get a decent C++ text. I think Koenig & Moo's Accellerated C++
will be very helpful to you.
Jul 19 '05 #3
>>
void main() {


main must return int.


why is that?
return to where?
the program compiles fine as it is.

--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------
Jul 19 '05 #4
>
void input_names(Cities[], int);


You don't need to repeat the declaration >here.


I am not repeating anything.
this is a prototype.
Cities info[numb_cities];


This is also not legal. Array declarations in C++ need sizes that are
constant
expressions (I bet you're using an anceint G++).


again, it compiled fine.
1st sentence of my post:
using Dev C++4
is it old? nope, but i do have borland c++ 1.0 from 91 which does give an
error.

--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------
Jul 19 '05 #5
Developwebsites wrote:
void main() {
main must return int.

why is that?


Because the standard requires it, as the ARM did before it.
return to where?
The calling environment.
the program compiles fine as it is.


regardless of the shortcomings of your compiler, it is required. Better
compilers will refuse it with the wrong return type.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #6
Developwebsites wrote:
void input_names(Cities[], int);
You don't need to repeat the declaration >here.

I am not repeating anything.
this is a prototype.


Yes, a prototype that appeared earlier in the code. Prototypes are
declarations.

Cities info[numb_cities];
This is also not legal. Array declarations in C++ need sizes that are
constant
expressions (I bet you're using an anceint G++).

again, it compiled fine.


Again, that doesn't matter. C++ is not "Whatever your compiler accepts".
All compilers accept some things that are not C++. gcc in particular
supports several language extensions by default. Learn how to invoke it
in standard compliant mode and it will diagnose your errors.
1st sentence of my post:
using Dev C++4


That comes with an old version of gcc.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #7

"Developwebsites" <de*************@aol.combatSPAM> wrote in message news:20***************************@mb-m19.aol.com...

void main() {
main must return int.


why is that?


The standard says so.
return to where?
The runtime environment.
the program compiles fine as it is.

Do not confuse tolerance with correctness.
Jul 19 '05 #8
>>>> Cities info[numb_cities];

This is also not legal. Array declarations in C++ need sizes that are
constant


the point of my program, and the function before the above statement, was to
have the user input the number of cities.
then, the user inputs city names and temps.
how can i have a constant if i dont know the number of cities the user will
enter.
it is only logical(as my code is), it makes no sense to have a constant if the
number of cities or employees etc. is unknown.
if it could be done in BASIC why not C++?

--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------
Jul 19 '05 #9
In article <20***************************@mb-m29.aol.com>,
de*************@aol.combatSPAM says...
> Cities info[numb_cities];

This is also not legal. Array declarations in C++ need sizes that are
constant


the point of my program, and the function before the above statement, was to
have the user input the number of cities.
then, the user inputs city names and temps.
how can i have a constant if i dont know the number of cities the user will
enter.
it is only logical(as my code is), it makes no sense to have a constant if the
number of cities or employees etc. is unknown.
if it could be done in BASIC why not C++?


It _can_ be done in C++, but it's done somewhat differently. First of
all, until you know what you're doing, it's probably just about as well
to forget that C++ has arrays at all, and instead always use std::vector
for the kinds of problems where you'd use arrays in BASIC.

Using std::vector, you don't need to pre-specify the number of cities at
all -- you can simply use push_back to add each city as it's entered,
and when the user enters a blank line (or some specific string like
"end", etc.) you quit adding cities to the collection.

As far as your code being logical, well, coming from BASIC it probably
seems that way -- but to the user it will undoubtedly seem supremely
ILlogical that you demand that s/he count the cities before entering the
data. In fact, counting items in a list is exactly the sort of thing
the computer is good at, and the use should NEVER have to do!

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #10
Developwebsites wrote:
> Cities info[numb_cities];

This is also not legal. Array declarations in C++ need sizes that are
constant


the point of my program, and the function before the above statement, was to
have the user input the number of cities.

Then you either need to use dynamic arrays (as in pointers to memory
allocated by new) or much much much preferably with std::vector.
If you want help on this newsgroup, you are going to have to stop
arguing with people and things that you don't about. There a number of
very knowledgable programmers here, so follow their advice. Don't use
the platform-specific extensions that you are told not to, instead use
the proper standard forms.

Brian Rodenborn
Jul 19 '05 #11

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

Similar topics

2
by: James S | last post by:
Hi, Basically I've been fighting with this code for a few days now and can't seem to work around this problem. Included is the output, the program I use to get this error and the source code for...
2
by: Bob | last post by:
Everybody, I've been doing a lot of on-line research and cannot find any reference to the exact problem I'm having. Let me preface this question with the fact that I'm coming from an Oracle...
1
by: MAHESH MANDHARE | last post by:
hi To all , I want to know about garbage collector & freeing memory of objects i have writened one sample program and in that i created one destructor something like this using System;...
2
by: Amrit Kohli | last post by:
Hello. I have the following code, to do a simple operation by copying the elements of a vector of strings into an array of char pointers. However, when I run this code, the first element in the...
8
by: mike2036 | last post by:
For some reason it appears that garbage collection is releasing an object that I'm still using. The object is declared in a module and instantiated within a class that is in turn instantiated by...
15
by: vk02720 | last post by:
Hi, I am trying to implement garbage collection for C/C++ mainly for learning purpose. I dont know where to start. For example - Basic questions - How do I identify the variables that contain...
12
by: leonard.guillaume | last post by:
Hi guys, I use dynamic char arrays and I'm trying to get rid of the garbage in it. Let me show you the code and then I'll explain more in details. ...
5
by: 7stud | last post by:
gc.garbage returns an empty list even though the command: gc.set_debug(gc.DEBUG_LEAK) produces the following output: gc: uncollectable <Dog 0x56e10> gc: uncollectable <Cat 0x56e30> gc:...
4
by: mac11 | last post by:
I'm having an issue where the output of the 'set' command gives me extra garbage and I'm wondering if anybody can help point me toward a solution. Actually, it looks like part of a script, not random...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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...

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.