Hi all,
I am trying to make a program but I have a problem. So I can't continue.

I am using a struct like that :

typedef struct account
{
    char fullname[40];
    int  id;
    int  amount;
    char name[15];
} account;


typedef struct parking_system
{
    int pos;
    char name[15];
    char description[80];
    float price;
    int busy_lots;
    account allaccounts[5];
    int id;
} parking_system;

parking_system *myaccounts;

I have a variable that saves a string like char name[]="NAME";
But when I am trying to pass this value to my struct and then to print it
it does not work.

strcpy( myaccounts->allaccounts[myaccounts->pos].name,name);       
myaccounts->pos ++;

printf("Name = \n",myaccounts->allaccounts[myaccounts->pos].name);

And another question how can I pass value to variable id for example using parking_system ?

What am I doing wrong ?
Please help!

>> But when I am trying to pass this value to my struct and then to print it it does not work.

You have forgotten the %s format specifier, so ..

strcpy( myaccounts->allaccounts[myaccounts->pos].name,name);
/* %s embedded in the format string */
printf("Name = %s\n",myaccounts->allaccounts[myaccounts->pos].name);

>> And another question how can I pass value to variable id for example using parking_system ?

Sorry, but I don't understand what that means.

Ninja-posted - blah! ;)

>> But when I am trying to pass this value to my struct and then to print it it does not work.

You have forgotten the %s format specifier, so ..

strcpy( myaccounts->allaccounts[myaccounts->pos].name,name);
/* %s embedded in the format string */
printf("Name = %s\n",myaccounts->allaccounts[myaccounts->pos].name);

>> And another question how can I pass value to variable id for example using parking_system ?

Sorry, but I don't understand what that means.

No I have %s in my code. Here in the code I forgot it.
Something else goes wrong.

>> No I have %s in my code. Here in the code I forgot it.

*Sigh* -- then I'm assuming that you may be incrementing the value of pos too soon (i.e. before printing).

For future reference, please

  • post the actual code that seems to be failing
  • and try to explain how the code fails, i.e. what you expect vs. what happens

>> No I have %s in my code. Here in the code I forgot it.

*Sigh* -- then I'm assuming that you may be incrementing the value of pos too soon (i.e. before printing).

For future reference, please

  • post the actual code that seems to be failing
  • and try to explain how the code fails, i.e. what you expect vs. what happens
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

int pos;

typedef struct account
{
    char fullname[40];
    int  id;
    int  amount;
    char name[15];
} account;


typedef struct parking_system
{
    int pos;
    char name[15];
    char description[80];
    float price;
    int busy_lots;
    account allaccounts[5];
    int id;
} parking_system;

parking_system *myaccounts;


int create(int socket)
{
    char name[15];
         			
    //here is code that read name from a client 
    // the value of variable name is printed ok
    //so I write it now like this                 		
    strcpy(name,"Parking_Name");					
    
    strcpy( myaccounts->allaccounts[myaccounts->pos].name,name);
    myaccounts->pos ++;
    printf("Name = %s\n",myaccounts->allaccounts[myaccounts->pos].name);
   	
   
return 1;
}


int main()
{
    
    create(1);
    
system("pause");    
return 0;    
}
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

int pos;

typedef struct account
{
    char fullname[40];
    int  id;
    int  amount;
    char name[15];
} account;


typedef struct parking_system
{
    int pos;
    char name[15];
    char description[80];
    float price;
    int busy_lots;
    account allaccounts[5];
    int id;
} parking_system;

parking_system *myaccounts;


int create(int socket)
{
    char name[15];
         			
    //here is code that read name from a client 
    // the value of variable name is printed ok
    //so I write it now like this                 		
    strcpy(name,"Parking_Name");					
    
    strcpy( myaccounts->allaccounts[myaccounts->pos].name,name);
    myaccounts->pos ++;
    printf("Name = %s\n",myaccounts->allaccounts[myaccounts->pos].name);
   	
   
return 1;
}


int main()
{
    
    create(1);
    
system("pause");    
return 0;    
}

See this code

strcpy( myaccounts->allaccounts[myaccounts->pos].name,name);

myaccounts->pos ++;

printf("Name = %s\n",myaccounts->allaccounts[myaccounts->pos].name);

What you are doing is
1. Copy the character array to another araray .ie allaccounts[[I]index[/I]].name
2. Increment index
3. Trying to print the value of character array after incrementing index in step2

which obviously will fail. Try swapping step 2 and 3.

See this code

strcpy( myaccounts->allaccounts[myaccounts->pos].name,name);

myaccounts->pos ++;

printf("Name = %s\n",myaccounts->allaccounts[myaccounts->pos].name);

What you are doing is
1. Copy the character array to another araray .ie allaccounts[[I]index[/I]].name
2. Increment index
3. Trying to print the value of character array after incrementing index in step2

which obviously will fail. Try swapping step 2 and 3.

Thanks a lot !!

Hello mitrmker... I was wondering if you could help me with my code. it doesnt seem to be working out for me and i cant figure out why. Also, I need to put a loop in at the end so that this program will process multiple orders... if there is anything you can do to help that would be great.

#include <fstream>
#include <iostream>
#include <string>
#include <iomanip.h>
using namespace std;
int main()
{
// Declare variables
int item, quantity;
double price = 0.0;
const int SIZE = 6;
int valid_item;
double valid_item_price[] = {0.89, 5.99, 7.50, 15.99, 19.50, 59.00};
int sub;
bool foundIt = false;
const string MSG_YES = "Item found";
const string MSG_NO = "Item not found";
ifstream data_in;
// Load the data into the array
// Initialize subscript to 0
sub = 0;
// Open input and output file
data_in.open("inputFile.dat");
// Loop through the file
while(!(data_in.eof()))
{
// Input the data value into the array
data_in >> valid_item[sub];
// Show the data was loaded
cout << valid_item[sub] << " was stored in the array " << endl;
sub += 1; // Increment loop index
}
data_in.close();
cout << endl;
// Get user input
cout << "Enter item number between 1 and 999: ";
cin >> item;
// Initialize subscript to 0
sub = 0;
cout << endl;
// Search the array for a match -- exit if item is found
while((sub < SIZE) && (!(foundIt)))
{
// Show the search
cout << valid_item[sub] << " was compared" << endl;
// Test to see if this item is the desired item
if(item == valid_item[sub])
{
foundIt = true; // Set flag to true
price = valid_item_price[sub]; // Get price from the array
}
sub += 1; // Increment subscript
}
// Test value of foundIt
if(foundIt) // BEGINNING OF IF-THEN ELSE
{
cout << endl;
cout << MSG_YES << endl;
cout << "Enter quantity: ";
cin >> quantity;
cout << setprecision (2)
<< setiosflags(ios::fixed | ios::showpoint);
cout << endl << quantity << " at $" << price << " each" << endl << endl;
cout << "Total: $" << quantity * price << endl <<endl;
}
else // END OF IF-THEN ELSE
cout << endl << MSG_NO << endl;
system("Pause");
return 0;
}

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.