LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I am really caught up (https://www.linuxquestions.org/questions/programming-9/i-am-really-caught-up-531291/)

Senatla 02-22-2007 05:08 AM

I am really caught up
 
Hello friends...........

Here is my code, I want to type in a number of names and at the end of that I want to store all tat in the vector, called names. And when it encounters a space i.e, entering no name, it should terminate but this seems to continue giving me to insert a name at infinitum.
so can u please help me here ................

Here is the code..........

#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main()
{
»·······vector <string> names;
»·······vector <int> theirmarks;
»·······int marks;
»·······string name;

»·······if(name != " ")
»·······{
»·······»·······string name;
»·······»·······cout<<"Enter student name:";
»·······»·······getline(cin,name);
»·······»·······if (name != " ")
»·······»·······{
»·······»·······»·······names.push_back(name);
»·······»·······}
»·······»·······while (name != " ")
»·······»·······»·······sort(names.begin(),names.end());
»·······»·······theirmarks.resize(names.size());
»·······}
»·······else if ( name == " ")
»·······{
cout<<"Illegal name"<<"\n"<<"The size of names is"<<names.size()<<endl;
}
}

phantom_cyph 02-22-2007 05:21 AM

i dont know a whole lot about programming(unless its html)-but could the fact that you have two opening brackets in a row not separated by a closed one?

int main()
{<----open
»·······vector <string> names;
»·······vector <int> theirmarks;
»·······int marks;
»·······string name;

»·······if(name != " ")
»·······{<----open
»·······»·······string name;
»·······»·······cout<<"Enter student name:";
»·······»·······getline(cin,name);
»·······»·······if (name != " ")
»·······»·······{<----open
»·······»·······»·······names.push_back(name);
»·······»·······}<----first close

just a suggestion...

graemef 02-22-2007 05:26 AM

Sorry to say it but your code is in a bit of a mess. You really need to start again. What I am looking at is the classic mistakes from someone new to coding. My request to you is to take it slowly and break the problem down. First can you write some code that will just loop ask for a name and stop looping when a single space is entered. That will help you to get the basic structure right. Once you have done that post the code. If you have any problems with that let me know and I can try and help you.

Remember the program will just loop ask for a name and stop looping when a single space is entered.

Don't do anything with the name yet, don't create any vectors. Include the minimum code that you need.

matthewg42 02-22-2007 05:45 AM

In LQ forums posts you can enclose code segments in the [CODE] tags, which will preserve formatting and use a fixed width font.

Minor note: main() should have params (int argc, char** argv), and should return an integer when it terminates.

OK, the actual problem: when you sort with this part:
Code:

while(name != " ")
    sort(names.begin(), names.end());

How do you expect it to end the loop? The sort function will not modify the value of name, so the condition for the loop will never change - you have an infinite loop, that is why the program gets stuck.

Also, having two separate containers - one for the marks and one for the student names is risky - the orders of the two would easily get out of sync, and you would have no way to recover. It would be better to use a map<string, int> object to relate the student name to their mark.


All times are GMT -5. The time now is 10:19 AM.