Programming using pointers and arrays....driving me nuts!

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 9031 Views - Last Post: 18 November 2012 - 08:12 PM Rate Topic: -----

#1 blondielox  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 24-August 11

Programming using pointers and arrays....driving me nuts!

Posted 05 November 2011 - 08:12 PM

I am working on a homework assignment:


Reference the following instructions to assist you when completing your Programming Using Arrays and Pointers assignment.

Using a for loop, print the contents of the array.

The output should appear like this:

PRINTING CONTENTS OF ARRAY
==================================
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z


Here is my code:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main()
{


char  letters [26] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

cout << "PRINTING CONTENTS OF ARRAY" << endl;
cout << "==================================" << endl;

for (int i = 0; i < sizeof(letters); ++i)
{
cout << letters << " ";
}


cout << endl;


system ("pause");
return 0;
}



and here is my output:

PRINTING CONTENTS OF ARRAY
==================================
ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXY
Z ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWX
YZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVW
XYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUV
WXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTU
VWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRST
UVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRS
TUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ
Press any key to continue . . .


Why is it printing the alphabet so many dang times !?!?!?!?!

Can someone tell me what I am missing here?

Is This A Good Question/Topic? 0
  • +

Replies To: Programming using pointers and arrays....driving me nuts!

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Programming using pointers and arrays....driving me nuts!

Posted 05 November 2011 - 08:20 PM

Copy and paste the exact line of code for printing output. Then explain the syntax of that line.
Was This Post Helpful? 0
  • +
  • -

#3 blondielox  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 24-August 11

Re: Programming using pointers and arrays....driving me nuts!

Posted 05 November 2011 - 08:25 PM

OK. Looked at it again.....added a break; statement in there after cout << letters << ""; and it worked! Now just hope I can figure out the rest of it....

Thanks for the second set of eyes!
Was This Post Helpful? 0
  • +
  • -

#4 blondielox  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 24-August 11

Re: Programming using pointers and arrays....driving me nuts!

Posted 05 November 2011 - 08:38 PM

Second part of the assignment:


Change the program logic to prompt you for a position within the array that coincides with the letter. See the example below:

This is the title to your Program related to the alphabet.

Select the number that coincides with the alphabet.
For example, the number 7 should display the letter G.

Enter a number between 1 and 26: 4

The number you selected: 4
The letter related to this number: D

My code:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main()
{


char  letters [26] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

cout << "PRINTING CONTENTS OF ARRAY" << endl;
cout << "==================================" << endl;

for (int i = 0; i < 26; ++i)
{
cout << letters << " ";
break;
}


cout << endl;

cout << "This is the title to your Program related to the alphabet." << endl;
cout << endl;
cout << "Select the number that coincides with the alphabet." << endl;
cout << "For example, the number 7 should display the letter G." << endl;
cout << endl;
cout << "Enter a number between 1 and 26: ";

int number;
cin >> number;

cout << endl;
cout << "The number you selected: " << number << endl;

if ((number > 0) && (number <= sizeof(letters)))
{
cout << "The letter related to this number: " << letters[number-1] << endl;
}
else
{
cout << "Sorry, the number must be between 1 and 26." << endl;

system ("pause");
return 0;
}
}




Problem I am having is that after I enter a valid number the window closes. I am assuming that I need to add another break statement but cannot figure out where it is supposed to be.
Was This Post Helpful? 0
  • +
  • -

#5 blondielox  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 24-August 11

Re: Programming using pointers and arrays....driving me nuts!

Posted 05 November 2011 - 08:53 PM

Nevermind.....got that one figured out too!
Was This Post Helpful? 0
  • +
  • -

#6 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Programming using pointers and arrays....driving me nuts!

Posted 05 November 2011 - 09:10 PM

> added a break; statement in there after cout << letters << ""; and it worked

This is wrong. Stop and think. You have a loop, and then you break immediately? Well, your real problem is that letters is not null terminated, so you cannot treat it as a C string. I.e. cout << letters expects a C string, and letters doesn't qualify. That the program works is by chance.
Was This Post Helpful? 0
  • +
  • -

#7 blondielox  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 24-August 11

Re: Programming using pointers and arrays....driving me nuts!

Posted 05 November 2011 - 09:12 PM

View PostOler1s, on 05 November 2011 - 11:10 PM, said:

> added a break; statement in there after cout << letters << ""; and it worked

This is wrong. Stop and think. You have a loop, and then you break immediately? Well, your real problem is that letters is not null terminated, so you cannot treat it as a C string. I.e. cout << letters expects a C string, and letters doesn't qualify. That the program works is by chance.


Not sure I am understanding what you are saying about null terminated??
Was This Post Helpful? 0
  • +
  • -

#8 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Programming using pointers and arrays....driving me nuts!

Posted 05 November 2011 - 09:28 PM

Quick correction on my part: I meant to say NUL terminated, not null terminated. NUL is represented by '\0' in C.

When you have: cout << letters you are passing an array of characters to be printed. There's a convention in C, and thus C++, that an array of characters, intended to be treated as a string, e.g. "Hello, World!" has a NUL character at the end. The NUL character helps identify the end of the string, so it's pretty important.

So, you have two choices. If you intend to treat letters as a C string, the last character must be '\0', or you must not treat it as a C string, and instead operate on the individual characters themselves.

Make sense?
Was This Post Helpful? 0
  • +
  • -

#9 blondielox  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 24-August 11

Re: Programming using pointers and arrays....driving me nuts!

Posted 05 November 2011 - 09:31 PM

Ah! I had forgotten about that! But where exactly do I put it in that line of code? Everything I just tried comes back with an error.

Oops! forgot to enclose it in the '' so its good now. I have removed the break statement and it works like it is supposed to.


#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main()
{


char  letters [] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '\0' } ;

cout << "PRINTING CONTENTS OF ARRAY" << endl;
cout << "==================================" << endl;

for (int i = 0; i < 26; ++i)
{
cout << letters << " ";

}


cout << endl;
cout << endl;


cout << "This is the title to your Program related to the alphabet." << endl;
cout << endl;
cout << "Select the number that coincides with the alphabet." << endl;
cout << endl;
cout << "For example, the number 7 should display the letter G." << endl;
cout << endl;
cout << "Enter a number between 1 and 26: ";

int number;
cin >> number;

cout << endl;
cout << "The number you selected: " << number << endl;

if ((number > 0) && (number <= sizeof(letters)))
{
cout << "The letter related to this number: " << letters[number-1] << endl;
system ("pause");
}
else
{
cout << "Sorry, the number must be between 1 and 26." << endl;

system ("pause");

cout <<endl;


           
}
			
	    
return 0;
}



This post has been edited by blondielox: 05 November 2011 - 09:34 PM

Was This Post Helpful? 0
  • +
  • -

#10 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Programming using pointers and arrays....driving me nuts!

Posted 05 November 2011 - 09:35 PM

- Scratch previous comment -

Looks like everything is fine then.

EDIT: Not really.

for (int i = 0; i < 26; ++i)
{
cout << letters << " ";

}



Hmm? You are going to print all the letters 26 times?

EDIT 2:

sizeof(letters) will be different now, yes?

This post has been edited by Oler1s: 05 November 2011 - 09:38 PM

Was This Post Helpful? 0
  • +
  • -

#11 blondielox  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 24-August 11

Re: Programming using pointers and arrays....driving me nuts!

Posted 05 November 2011 - 09:40 PM

I have gotten all the other problems cleared up but now I am struggling with part 3:


Then, write the code to update every other element within the array with a lowercase x. The output should appear like the following:

PRINTING CONTENTS OF ARRAY and adding x to every other element
A x C x E x G x I x K x M x O x Q x S x U x W x Y x

I have tried:

int i = 0

for (i = 0; i < 26; i = i +2)
{
cout << letters 'x';
}



It isn't working so I am not doing it right. Any suggestions here?
Was This Post Helpful? 0
  • +
  • -

#12 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Programming using pointers and arrays....driving me nuts!

Posted 05 November 2011 - 09:41 PM

> cout << letters 'x';

And what is this syntax supposed to be??

Letters is an array. How do you get one of elements of the array?
Was This Post Helpful? 0
  • +
  • -

#13 blondielox  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 24-August 11

Re: Programming using pointers and arrays....driving me nuts!

Posted 05 November 2011 - 09:49 PM

That is where I am struggling. For some reason I am not understanding this part. Maybe because I am trying too hard and am tired or it just isn't making any sense at the moment.
Was This Post Helpful? 0
  • +
  • -

#14 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Programming using pointers and arrays....driving me nuts!

Posted 05 November 2011 - 09:53 PM

When a problem is hard to solve, break it down into smaller components you can solve. Don't hope for brilliant inspiration, because that's unlikely.

You know you have a print each letter. That's two issues: 1) printing and 2) a letter.

So focus on how to get a letter. Do you or do you not know how to get a single letter? If you don't, then the question is how do you find out?

How about Google? Here's the first search result from Google: http://www.cplusplus...utorial/arrays/. Read the section on how to access values in the array.
Was This Post Helpful? 0
  • +
  • -

#15 heyoman1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 64
  • View blog
  • Posts: 735
  • Joined: 30-November 09

Re: Programming using pointers and arrays....driving me nuts!

Posted 05 November 2011 - 10:00 PM

Think about it. You want to print out an element of an array in a sequence ("A B C..."). Your code is:

cout << letters << " ";



Your problem is that you are printing the whole array at once, and not breaking it down.

Here's my hint:
Figure out how to access individual elements, and use the "i" in the for loop to help.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3