Using a for loop to display the contents of an array

S

Scott

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

}
 
K

karthik kumar

Scott said:
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.
 
N

Niels Dybdahl

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
 
M

Martin Magnusson

Scott said:
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
 
G

Gianguz

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
 
J

Jerry Coffin

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

Ron Natalie

iostream.h is a non-standard header name. It frequently doesn't
boogie well with objects defined in standard headers.
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.
 

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
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top