Thread: whats wrong with my code?

  1. #1
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161

    Exclamation whats wrong with my code?

    anyone tell me whats wrong? is it about using vector with structure?

    #include <stdio.h>
    #include <fstream>
    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
    int main(int argc, char *argv[])
    {
    struct stu{
    string name;
    int age;
    };
    int no=0;
    char other;
    vector<stu> school;
    do {
    cout<<"enter the name ";
    cin>>school[no].name;
    cout<<"enter age";
    cin>>school[no].age;
    cout<<"another?";
    cin>>other;
    no+=1; }
    while (other=='y');
    }
    Programming is a high logical enjoyable art for both programer and user !!

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You'll have to move

    struct stu{
    string name;
    int age;
    };

    out of main(). Also your vector has no initial size. Either you'll have to initialise the amount of stu's it can hold in it's constructor (and make sure you don't overflow) or fill a temporary stu from the input and the use the push_back() method to dynamically add elements (which will copy your temporary stu into the vector).

  3. #3
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161

    Smile

    thank you its working now, but i was wondering why should i put the atructure outside the function main?
    Thank you again!!
    Programming is a high logical enjoyable art for both programer and user !!

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >why should i put the atructure outside the function main

    It needs to be declared globally to be used as a template parameter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM