Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,442 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,323 people online right now. Registration is fast and FREE... Join Now!




Output limited quantity

 
Reply to this topicStart new topic

Output limited quantity

Max_Payne
6 Jan, 2008 - 09:32 PM
Post #1

D.I.C Head
**

Joined: 19 Oct, 2007
Posts: 51


My Contributions
In this program i enter the names of 40 friends.

I need to display the names 15 at a time, pausing before continuing with the list.
Still haven't figured out a way to do it.

So far this is what i could come up with:

CODE

#include <iostream>
#include <conio.h>
#include <iomanip>

int main ()
{
    const int maxFriends = 40;
    char friends [maxFriends];

    for (int i = 0; i < maxFriends; ++i)
    {
        std::cout << "Enter name #" << (i + 1) << " ";
        std::cin.get (friends[i]);
        std::cin.ignore (100, '\n');

        system("CLS");
    }

    for (int i = 0; i < maxFriends; ++i)
    {
        std::cout << friends[i] << std::endl;

        if (i == 15)
        {
            std::cout << "Press anykey to continue";
            _getch();
        }
    }

    _getch();
}


This post has been edited by Max_Payne: 6 Jan, 2008 - 09:34 PM
User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: Output Limited Quantity
6 Jan, 2008 - 09:49 PM
Post #2

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

Well, right now it is pausing after the first 15 but not after 30 it looks like. I think if you change your "if" statement to this:



CODE

if (i% 15 == 0 && i != 0)



it will execute the get character pause every 15 names.

User is offlineProfile CardPM
+Quote Post

Max_Payne
RE: Output Limited Quantity
6 Jan, 2008 - 09:53 PM
Post #3

D.I.C Head
**

Joined: 19 Oct, 2007
Posts: 51


My Contributions
Do you also know why when i input for example:
JENIFER it only outputs the first letter. 'J'

This post has been edited by Max_Payne: 6 Jan, 2008 - 09:54 PM
User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: Output Limited Quantity
6 Jan, 2008 - 10:12 PM
Post #4

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

QUOTE(Max_Payne @ 6 Jan, 2008 - 10:53 PM) *

Do you also know why when i input for example:
JENIFER it only outputs the first letter. 'J'




You've set up an array of characters and you are using "get", which I believe only gets one character. I think you want an array of strings or c-strings rather than an array of characters. Try this:

CODE

#include <string>   // add this line to get string library




#include <iostream>
#include <conio.h>
#include <iomanip>

int main ()
{
    const int maxFriends = 40;
    string friends [maxFriends];   // change "char" to "string"

    for (int i = 0; i < maxFriends; ++i)
    {
        std::cout << "Enter name #" << (i + 1) << " ";
        std::cin >> friends[i];


//       std::cin.ignore (100, '\n');        you can remove this line I think

        system("CLS");
    }

    for (int i = 0; i < maxFriends; ++i)
    {
        std::cout << friends[i] << std::endl;

        if (i % 15 == 0 && i != 0)
        {
            std::cout << "Press anykey to continue";
            _getch();
        }
    }

    std::cin >> friends[i];
}




I'm pretty sure "get" only gets a character at a time. "getline" will get an entire line and you may consider that if you want first and last names. Otherwise, plain old "cin" works. Regardless, as you had it, you were only allocating space for forty characters total before and you need more for forty names.

I'm not sure why you were using "ignore". I don't think you need the
system ("CLS") command in there either, at least not inside the for-loop. I'm not too familiar with it or "ignore" so there may be a reason.





User is offlineProfile CardPM
+Quote Post

Max_Payne
RE: Output Limited Quantity
6 Jan, 2008 - 10:29 PM
Post #5

D.I.C Head
**

Joined: 19 Oct, 2007
Posts: 51


My Contributions
cin.ignore () is used to consume all potential additional letters of a name.

EDITED:::

I need to use C-Style strings... not C++ Style strings..

This post has been edited by Max_Payne: 6 Jan, 2008 - 10:42 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Output Limited Quantity
6 Jan, 2008 - 10:41 PM
Post #6

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
The only errors associated with that code have nothing to do with the actual use of the string - the errors are related to the lack of the inclusion of the proper <string> header, some scoping issues, and the lack of a proper namespace.

The problem with your original code is that you aren't using an array of C-strings, you're using an array of characters. To get an array of C-strings (null-terminated character arrays), you'd need to use a 2-dimensional array of characters:
CODE

const int maxFriends = 40;
const int maxNameLength = 20;
char friends [maxFriends][maxNameLength];


This will require changing some additional parts of your code. Is this more like what you're trying to do?

This post has been edited by jjhaag: 6 Jan, 2008 - 10:52 PM
User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: Output Limited Quantity
6 Jan, 2008 - 10:53 PM
Post #7

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

Just ran the code. Ran fine when I added "using namespace std" and took out the other namespace stuff. I think I needed "std::string" or something instead of "string" without using "namespace std". I did notice that it prints 16 names instead of 15 at first. To fix, you can either put the if statement before the printing or subtract 1 from i in the if statement. C-string or string, whichever you prefer. jjhaag is correct that you need a 2-D array since a C-string is a one dimensional array of characters and you need an array of C-strings, which makes it two dimensional.



User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: Output Limited Quantity
6 Jan, 2008 - 10:59 PM
Post #8

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

You can change the last line back to your line: _getch();


My i variable was out of scope, as jjhaag pointed out. Hadn't meant to put that line there.

User is offlineProfile CardPM
+Quote Post

Max_Payne
RE: Output Limited Quantity
7 Jan, 2008 - 06:07 AM
Post #9

D.I.C Head
**

Joined: 19 Oct, 2007
Posts: 51


My Contributions
QUOTE(jjhaag @ 6 Jan, 2008 - 11:41 PM) *



This will require changing some additional parts of your code. Is this more like what you're trying to do?


Probably. Could you give me a simple example. for example the input ?
User is offlineProfile CardPM
+Quote Post

Max_Payne
RE: Output Limited Quantity
7 Jan, 2008 - 06:28 AM
Post #10

D.I.C Head
**

Joined: 19 Oct, 2007
Posts: 51


My Contributions
QUOTE(VernonDozier @ 6 Jan, 2008 - 11:59 PM) *

You can change the last line back to your line: _getch();


My i variable was out of scope, as jjhaag pointed out. Hadn't meant to put that line there.


Even if i use the script i still need to use a variable for the number of names and a variable for the max characters per name. I just cant think of a way to do it but using a double for but that will make it two inputs..
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Output Limited Quantity
7 Jan, 2008 - 07:24 AM
Post #11

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
I'm not sure what you mean by that. Yes, you need a (const) variable for each of those terms. But why on earth would you need a double? Both the number of friends and the lengths of their names are integers. When using C-strings, you generally don't use the exact length of the the inputted string as the length of the character array that you are storing into - you use a buffer that is longer than the maximum expected length of the string.

This was from a previous post in this thread:
CODE
const int maxFriends = 40;
const int maxNameLength = 20;
char friends [maxFriends][maxNameLength];

This creates an array 40 elements long, where each element is ANOTHER array, 20 characters long. The first index into the array gives you the friend number. And the syntax for the get() method is given by:
CODE
istream& get( char* buffer, streamsize num, char delim );


So to store the name of the 5th friend, you would use:
CODE
cin.get(friends[4], maxNameLength, '\n');

This tells the cin object to pull in a maximum of maxNameLength characters into the 5th row (zero-based arrays in C/C++, remember) of the array. The '\n' delimiter tells it to stop reading when a newline character is found in the input buffer.

If you expect that the names are going to be longer than 20 characters, change that const int variable to something bigger - most first or last names are shorter than 20 characters, but if you feel you need to go with a bigger buffer, go for it.

This post has been edited by jjhaag: 7 Jan, 2008 - 07:25 AM
User is offlineProfile CardPM
+Quote Post

Max_Payne
RE: Output Limited Quantity
7 Jan, 2008 - 07:37 AM
Post #12

D.I.C Head
**

Joined: 19 Oct, 2007
Posts: 51


My Contributions
when i said
QUOTE
using a double for

i meant
CODE

for ()
{
     for ();
}


Anyways thank you both for the help...
Cheers.. smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 02:13PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month