Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using a for loop to display the contents of an array

Reply
Thread Tools

Using a for loop to display the contents of an array

 
 
Scott
Guest
Posts: n/a
 
      12-03-2004
Will someone please tell me why the first element of the array is not
displayed if the const int amt (see code below) is declared as a
multiple of 4?
If I declare it as 3, or 5 the all elements of the the array are
displayed, if 4 or a multiple is used, it will not cout the first
element.

#include <stdlib.h>
#include <string>
#include <iostream.h>

using namespace std;


void main(){
const int Amt = 4;
char r1[Amt];
char r2[Amt];


cout << "1: " << endl;
cin >> r1;
for (int x = 0; x < Amt; x++)
cout << r1[x];

cout << endl;

cout << "2: " << endl;

cin >> r2;
for (int y = 0; y < Amt; y++)
cout << r1[y];

cout << r1[0];

}
 
Reply With Quote
 
 
 
 
karthik kumar
Guest
Posts: n/a
 
      12-03-2004
Scott wrote:
> Will someone please tell me why the first element of the array is not
> displayed if the const int amt (see code below) is declared as a
> multiple of 4?
> If I declare it as 3, or 5 the all elements of the the array are
> displayed, if 4 or a multiple is used, it will not cout the first
> element.
>
> #include <stdlib.h>
> #include <string>
> #include <iostream.h>
>
> using namespace std;
>
>
> void main(){
> const int Amt = 4;
> char r1[Amt];
> char r2[Amt];
>
>
> cout << "1: " << endl;
> cin >> r1;
> for (int x = 0; x < Amt; x++)
> cout << r1[x];
>
> cout << endl;
>
> cout << "2: " << endl;
>
> cin >> r2;
> for (int y = 0; y < Amt; y++)
> cout << r1[y];
>
> cout << r1[0];
>
> }


And yes, do use size_t for array indices instead of int.

--
Karthik.
 
Reply With Quote
 
 
 
 
Niels Dybdahl
Guest
Posts: n/a
 
      12-03-2004
> Will someone please tell me why the first element of the array is not
> displayed if the const int amt (see code below) is declared as a
> multiple of 4?


Use a debugger.

Niels Dybdahl


 
Reply With Quote
 
Martin Magnusson
Guest
Posts: n/a
 
      12-03-2004
Scott wrote:
> Will someone please tell me why the first element of the array is not
> displayed if the const int amt (see code below) is declared as a
> multiple of 4?
> If I declare it as 3, or 5 the all elements of the the array are
> displayed, if 4 or a multiple is used, it will not cout the first
> element.
>
> #include <stdlib.h>
> #include <string>
> #include <iostream.h>
>
> using namespace std;
>
>
> void main(){
> const int Amt = 4;
> char r1[Amt];
> char r2[Amt];
>
>
> cout << "1: " << endl;
> cin >> r1;
> for (int x = 0; x < Amt; x++)
> cout << r1[x];
>
> cout << endl;
>
> cout << "2: " << endl;
>
> cin >> r2;
> for (int y = 0; y < Amt; y++)
> cout << r1[y];
>
> cout << r1[0];
>
> }



It does for me. However, you never put anything in something other than
r1[0] and r2[0]. Try initializing all elements that you try to print
before you print them and see if that helps.

/ martin
 
Reply With Quote
 
Gianguz
Guest
Posts: n/a
 
      12-03-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Scott) wrote in message news:<(E-Mail Removed). com>...
> Will someone please tell me why the first element of the array is not
> displayed if the const int amt (see code below) is declared as a
> multiple of 4?
> If I declare it as 3, or 5 the all elements of the the array are
> displayed, if 4 or a multiple is used, it will not cout the first
> element.
>
> #include <stdlib.h>
> #include <string>
> #include <iostream.h>
>
> using namespace std;
>
>
> void main(){
> const int Amt = 4;
> char r1[Amt];
> char r2[Amt];
>
>
> cout << "1: " << endl;
> cin >> r1;
> for (int x = 0; x < Amt; x++)
> cout << r1[x];
>
> cout << endl;
>
> cout << "2: " << endl;
>
> cin >> r2;
> for (int y = 0; y < Amt; y++)
> cout << r1[y];
>
> cout << r1[0];
>
> }


I tried your code with "gcc version 3.3.4 20040623 / Gentoo Linux"
and seems to work even if the compiler warn me about <iostream.h>
(<iostream> must be used) deprecation and error me on void main()
(i.e.: C++ standard requires int main() )
First element is always displayed as the whole array.
With compiler and platform are you using?

Gianguglielmo
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      12-03-2004
(E-Mail Removed) (Scott) wrote in message news:<(E-Mail Removed). com>...
> Will someone please tell me why the first element of the array is not
> displayed if the const int amt (see code below) is declared as a
> multiple of 4?
> If I declare it as 3, or 5 the all elements of the the array are
> displayed, if 4 or a multiple is used, it will not cout the first
> element.


let's start with "why are you using arrays?"

> #include <stdlib.h>
> #include <string>
> #include <iostream.h>
>
> using namespace std;
>
>
> void main(){


main returns an int -- right now, your entire program has undefined
behavior.

> const int Amt = 4;
> char r1[Amt];
> char r2[Amt];
>
>
> cout << "1: " << endl;
> cin >> r1;


This is dangerous -- your array only has room for 3 characters of
input (plus a NUL terminator) but you're allowing the user to enter an
arbitrary amount of input.

If you're going to work with strings, why not USE strings to do it?

std::string r1, r2;

std::getline(std::cin, r1);
std::cout << r1 << std::endl << "2: ";

std::getline(std::cin, r2);
std::cout << r2;

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      12-03-2004
Jerry Coffin wrote:

>
>>#include <stdlib.h>
>>#include <string>
>>#include <iostream.h>
>>


iostream.h is a non-standard header name. It frequently doesn't
boogie well with objects defined in standard headers.

>>
>>void main(){

>
>
> main returns an int -- right now, your entire program has undefined
> behavior.
>


Actually, it's ill-formed. The compiler should issue a diagnostic.
There may be an implementation defined extension to allow you to progress.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Printing out contents of 2 arrays using a loop Paul Roche Ruby 4 11-02-2010 05:25 PM
if innerHTML used twice then it replaces the contents i want it to display all the contents virendra.amritkar@gmail.com Javascript 0 06-29-2007 08:13 AM
How to display binary file contents if it is stored in an integer array? loudking C Programming 9 05-01-2007 05:26 PM
help: loop within a loop -how to display data in 3 columns by 4 rows format addi ASP General 0 12-31-2003 04:31 AM



Advertisments