this would be my first time with arrays so i still have no idea how to use it
i'm thinking on using it on this program

#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;

int main()
{
    char main_menu;
    char parts;
    char items[]; //here's the part i don't know yet
first_page:
    cout<<"1 - Buy\n\n";
    main_menu = getch();
    switch (main_menu) {
           case '1':
                system("cls");
                cout<<"Parts\n\n0 - quadcore\n1 - dual core";
                parts = getch();
                switch (parts) {
                       case '0':
                            strcpy(items[],"Quadcore"); // assignment of a constant string to a variable
                            break;
                       case '1':
                            strcpy(items[],"Dual Core");
                            break;
                       default:
                            cout<<"\n\nInvalid";
                            break; }
                break;
           default:
                goto first_page;
                break; }
return 0;
}

this program is not yet done obviously (not to mention it does not work,yet)

how do i use these "arrays" in this program together with the strcpy?

what i want to happen is the program will display at the end what the user bought

for example:

You Bought:

Quadcore
Dualcore
Quadcore

maybe what i need with this program are not arrays

someone who know some other way on how i can produce the output i want?

char item_purchased1;
char item_purchased2;
char item_purchased3;
//and so on...

//then

parts = getch();
switch (parts) {
case '0':
strcpy(item_purchased1,"Quadcore");
break;
case '1':
strcpy(item_purchased2,"Dualcore");
break;

//then after an input
//the variable will be item_purchased2

what's the simplest way to solve this problem?

Question: Does the user buy one item or three items? It appears that the user buys three items. Regardless it looks like you are trying to allocate one character to store "Quadcore". This is not going to work. I think you want to work with strings. You'll have to decide whether you want to use C++ style strings or C style strings. You also have some old style headers (with ".h" on the end). You may want to get rid of "string.h" and use "string" instead. Also, do you really need/want "conio.h"? You have the iostream library. It seems like that will do what you want and you can get rid of "conio.h" altogether.

I would change item_purchased1 and the other two item_purchased variables to be of type string instead of char.

the user can buy as much items as he wants

then items bought be displayed after he's done choosing

that's the part i'm having a problem because the number of items being purchased depends on the user


i'm using conio.h for the getch()

Okay, if the user can buy as much as he/she wants, I think you need an array or a vector. Otherwise, if the user wants to buy 100 things, do you want to declare 100 separate variables? Arrays actually aren't so bad.

I think you can use the cin.get (char&) from iostream and toss conio.h out. You'll have to decide how the user decides how many times to go through the loop for a purchase. Does he/she type in that number at the very beginning? Or type a sentinel flag to indicate he/she is done? The answer to that will help decide how to design your loop. You'll want your switch statement inside that loop.

yes,that's what i thought,that arrays would do the trick

but the problem is i can't understand clearly how to use arrays. mind showing me how?it would be greatly appreciated.

in my case statements,i will put a case 'x' that will tell the program that the user is done

is the cin.get similar with the getch where the user doesn't need to press enter to input?

Check out the earlier poster's link on arrays. Here's another one:http://www.cplusplus.com/doc/tutorial/arrays.html

I assume you are going to use a static array rather than a dynamic one. The downside of the static array is that you'll have to declare the size of it ahead of time. You can try to decide what the maximum number of items a person can buy and make your array at least that big. You also need to decide whether you want to use C-Style strings or C++ style strings. If you go the C++ style route you'll declare it as something like this:

const int MAX_ITEMS_BOUGHT = 100;
string item_purchased[MAX_ITEMS_BOUGHT];

This sets aside space for 100 strings. If the user tries to buy 101 items, you'll have problems. That's if you go the C++ style string route. If you go the C style string route, you'd declare it something like this:

const int MIB = 100; // MIB = "max items bought"
const int MSL = 50;  // MSL = "max string length"
char item_purchased[MIB][MSL];

You'll have problems if the user buys more than 100 items or if an item is more than 49 characters.

As far as cin.get goes, you have to hit the return key. There's probably another command where you don't need to but I can't think of it off the top of my head. Anyway everyone always says conio.h is deprecated.

const int max_items=100;
    string items[max_items];
          
parts_page:
                cout<<"Parts\n\n0 - quadcore\n1 - dual core";
                parts = getch();
                switch (parts) {
                       case '0':
                            for(int n=0;n<=max_items;n++)
                            items[n]="Quadcore";
                            goto parts_page;
                            break;
                       case '1':
                            for(int n=0;n<=max_items;n++)
                            items[n]="Dual Core";
                            goto parts_page;
                            break;
                       case 'x':
                            cout<<"\n\nitems bought:\n\n"<<items<<endl;
                            break;
                       default:
                            cout<<"\n\nInvalid";
                            break; }

i really am having a hard time understanding arrays
am i even getting close?

I think you have your for-loop in the wrong place. Are they buyinging 100 of the same item?. Also in this line (line 20):

cout<<"\n\nitems bought:\n\n"<<items<<endl;

you've now named the variable "items" to be an array of strings. I'm not sure what you want the above line to print out, but I don't think it's going to do what you want.

I think you want the shopper to buy one item every time he/she enters a character. Is that correct? If so you do not want the loops that you have within the switch statement. Also I think you should get rid of those goto statements and change it to a for or while loop. However, all that said, this statement:

items[n]="Dual Core";

is a correct assignment command for a string array element, so you have that part right.

I think you want the shopper to buy one item every time he/she enters a character. Is that correct?

yes

case '1':
               for(int n=0;parts!='x';n++) {
                cout<<"Parts\n\n0 - quadcore\n1 - dual core";
                parts = getch();
                
                switch (parts) {
                       case '0':
                            items_temp[n]="Quadcore";
                            strcpy(items,items_temp);
                            break;
                       case '1':
                            items_temp[n]="Dual Core";
                            strcpy(items,items_temp);
                            break;
                       case 'x':
                            cout<<"\n\nitems bought:\n\n"<<items<<endl;
                            break;
                       default:
                            cout<<"\n\nInvalid";
                            break; } }

you've now named the variable "items" to be an array of strings. I'm not sure what you want the above line to print out, but I don't think it's going to do what you want.

what i wanted with that part was when the user entered 'x',everything bought will be displayed.
for example, the user pressed 0 then 0 then 1 then 1 then x
the output would be

Items bought:

Quadcore
Quadcore
Dualcore
Dualcore

i've never used STRINGS to be a variable's data type before so i do not know how to
have the output i wanted. i've searched the net and the closest i've got is strcpy and strncpy,tried both,but can't make it work.

You don't need to use strcpy or anything like that and you don't need to use a "temp" variable. Just assign the value to the string directly like you did the last time. You had it right before with this line:

items[n]="Dual Core";

No temporary variables needed. No strcpy needed. I have no idea where the line on line 1 came from:

case '1':

Do have another switch statement somewhere before that line? If not, get rid of that line.
To display the entire array, you'll need to display one item at a time, one cout statement per item. To display the ith item, you'll do this:

cout << items[i] << endl;

You'll need to do the above statement for each item.

see following example it may help you:

char chStr[10][10];
		
//populate array with strings
		for(int i=0;i<10;i++)
		{
			strcpy(chStr[i],"hi");
		}
//print the strings 
		for(int i=0;i<10;i++)
		{
			printf("\n %s",chStr[i]);
		}

yeah there's a switch before that
i didn't notice i included the case '1'

anyway

for(;parts!='x';n++) {

                cout<<"Parts\n\n0 - quadcore\n1 - dual core";
                parts = getch();
                
                switch (parts) {
                       case '0':
                            items[n]="Quadcore";
                            cout<<"\n\n"<<items[n]<<endl;
                            break;
                       case '1':
                            items[n]="Dual Core";
                            cout<<"\n\n"<<items[n]<<endl;
                            break;
                       case 'x':
                            cout<<"items bought:\n\n";
                            break;

this only displays the item bought
how can i display ALL the items bought at the end after the user presses 'x'?

ahhh i got it!!!!

cout<<"Items bought:\n\n";
for (;n!=0;n--)
cout<<items[n]<<endl;

i have a problem not related with arrays but decided to ask in this same thread anyway
because i think this question is not worthy of a new thread.

sorry for this mess but i think pasting here my entire code will help you understand and solve my problem

which is:

after choosing the processor i want (ex: i pressed 0 which is equivalent to Q6600)
i will then need to press 'x' to go back to the parts menu
then when i'll try to input something(ex: 0 which should lead me to the processor page again) it will directly display the items bought then end the program.

i found out that after pressing 'x' then going to the parts menu,the next input is taken by the program as a case that didn't match anything (or default if i put a default in there) in the switch(main_menu)

how can i fix this one?
were those LABELS the ones causing the problem?

int main()
{
   

first_page:
    system("cls");
    cout<<"0 - FAQs\n1 - Buy\n\n";
    main_menu=getch();
//************start of SWITCH MAIN MENU************
    switch (main_menu) {
           case '0':
          /*faq page will be displayed*/ break;

           case '1':
                parts_menu:
                system("cls");
                cout<<"What do you want to buy?\n\n0 - Processor\n1 - Motherboard\n2 - Video Card\n3 - Memory\n4 - Harddisk drive\n5 - Optical Disk Drive\n6 - PowerSupply Unit\n7 - AVR\n8 - Monitor\n";
                parts=getch();
//************start of SWITCH PARTS************
                switch (parts) {
                       case '0':
                            proc_page:
                            for(;proc!='x';n++) {
                            system("cls");
                            cout<<"Processor\n\n0 - Q6600 2.4Ghz\t\tPhp13,610.00\n1 - C2D E6850 3Ghz\t\tPhp13,510.00\n2 - C2D E6750 2.66Ghz\t\tPhp 9,295.00\n3 - C2D E6550 2.13Ghz\t\tPhp 8,320.00\n\n\nPress x to go back\n";
                            proc=getch();
                            switch (proc) {
                              case '0':
                                   items[n]="Q6600 2.4Ghz\t\tPhp13,610.00";
                                   amount = 13610;
                                   break;
                              case '1':
                                   items[n]="C2D E6850 3Ghz\t\tPhp13,510.00";
                                   amount = 13510;
                              break;
                              case '2':
                                   items[n]="C2D E6750 2.66Ghz\t\tPhp 9,295.00";
                                   amount = 9295;
                              break;
                              case '3':
                                   items[n]="C2D E6550 2.13Ghz\t\tPhp 8,320.00";
                              break;
                              case 'x':
                                   goto parts_menu;
                                   fflush(stdin);
                                   break;
                              default:
                                   cout<<"\n\nInvalid";
                                   goto proc_page;
                                   break; }/*switch proc*/ } /*for loop*/ }/*switch parts*/
//************end of SWITCH PARTS************
                  break; }  //case 1 main menu break
                             
                
                int y = n;
                      cout<<"\nItems bought:\n\n";
                      for(n=0;n<=y;n++)
                      cout<<items[n]<<endl;

ughh

took me 2 hours to fix it myself
but finally i got it working

anyway

thanks alot,DangerDev

i now have ideas on arrays

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.