473,324 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

It gives me error in the initialization of array along with lot more errors.I tried a

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5. int main()
  6. {    
  7.     struct data
  8.     {
  9.         string country;
  10.         string fathername;
  11.         int   age;
  12.     };
  13.         data NAME;
  14.  
  15.     data name[5]={"Ali", "Usman", "Sarmad", "Awais", "Junaid"};
  16.     name[0]=(struct data){"Saudi Arabia","Tahir Awan",24};
  17.     name[1]=(struct data){"England","Akram Khan",20};
  18.     name[2]=(struct data){"China","Amjad Ali",20};
  19.     name[3]=(struct data){"Syria","Ahmad Ali",19};
  20.     name[4]=(struct data){"Oman","Zohaib Sultan",24};
  21.  
  22.             cout << "Enter Name of student/n";
  23.             cin >>  NAME;
  24.  
  25.     int j;
  26.     for(int i=0; i<6; i++)
  27.     {
  28.         if(NAME==name[i])
  29.         j=i;
  30.     }
  31.     cout << "We have following data for   " << name[j]<< endl;
  32.     cout << "Country:  " << name[j].country << endl << "Father name:  " << name[j].fathername << endl << "Age:  " << name[j].age << endl;
  33.  
  34.     return 0;
  35.  
  36. }
May 28 '15 #1
1 1231
weaknessforcats
9,208 Expert Mod 8TB
First, you can't use a C syntax to initialize a struct object. You will need to write an assign function.

Expand|Select|Wrap|Line Numbers
  1. struct data
  2. {
  3.     string country;
  4.     string fathername;
  5.     int   age;
  6.  
  7.     data assign(string, string, int);
  8.  
  9. };
  10. data data::assign(string country, string father, int age)
  11. {
  12.     data temp;
  13.     temp.country = country;
  14.     temp.fathername = father;
  15.     temp.age = age;
  16.     return temp;
  17. }
To get this to work you will need to define your struct and the assign function outside of main.

You use this code by:

Expand|Select|Wrap|Line Numbers
  1. name[0].assign("a", "b", 12);
  2.  
When you do:

Expand|Select|Wrap|Line Numbers
  1. data name;
  2.      cin >> name;
There is no operator >> that works with your struct. You will need to write one:

Expand|Select|Wrap|Line Numbers
  1. istream& operator>>(istream& input, data& obj)
  2. {
  3.  
  4.     input >> obj.country >> obj.fathername >> obj.age;
  5.  
  6.     return input;
  7. }
To use this code you just:

Expand|Select|Wrap|Line Numbers
  1. cin >> name[0];
A tip on fixing errors is to fix only the first error and recompile. That first error may be causing other errors, which all go away when the first error is fixed.

Let me know if any of this helps you out.
May 28 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: eje | last post by:
IsNumeric(value) should return false if value "can not be successfully converted to a Double." Instead I get the following error message: "Input string was not in a correct format." I use the...
6
by: vips | last post by:
Page_Load datagrid1.datasource=dataset1 //I am filling the datagrid and it works fine when page is displayed end ---------------
0
by: Pixie | last post by:
We are successfully getting a handle to a drive using createfile then using that handle to query the change journal using DeviceIOControl with the paramter FSCTL_QUERY_USN_DATA. However when we try...
12
by: arnuld | last post by:
in C++ Primer 4/3 Lippman says in chapter 3, section 3.3.1: vector<stringsvec(10); // 10 elements, each an empty string here is the the code output & output from my Debian box running "gcc...
6
by: ranesmitas | last post by:
Hi ,I am using Visual Basic Backend SQL I create form in which i create Command button NEW ,MODIFY, VIEW,SAVE,EXIT all my programme is running but when i add new record and save and then i...
4
by: Lindsay Bradley | last post by:
I have code built into a form in Access and everytime I try to open the form it gives me a Compile error: Expected array message and then highlights the word Left. I did not create this program and...
1
by: Cdweller | last post by:
Below is my code snippet.I'm getting "Error:initialization from incompatible pointer type" error on line 'int *q = status;'. Obviously, I'm missing something but has no clue...:( Could someone...
2
by: kkshansid | last post by:
I dragged text box and write in data/control source = & "," & Chr$(13) & Chr$(10) & & Chr$(13) & Chr$(10) & but it gives error that #name? in layout view kindly help to resolve the problem...
0
by: rahulsankhe1089 | last post by:
I am using below javascript <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script> <script type="text/javascript"> ...
3
by: rahulsankhe1089 | last post by:
i have created web application in asp.net using visual studio 2010. i did coding in vb.net. in vb.net code i used simple message box. it does not give error when i run project through visual...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.