LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2007, 05:08 AM   #1
Senatla
LQ Newbie
 
Registered: Jan 2007
Location: Lesotho in Africa, landlocked by South Africa
Distribution: scientific linux
Posts: 7

Rep: Reputation: 0
Question 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;
}
}
 
Old 02-22-2007, 05:21 AM   #2
phantom_cyph
Senior Member
 
Registered: Feb 2007
Location: My HDD...
Distribution: WinXP for designing, Linux for life.
Posts: 2,329
Blog Entries: 1

Rep: Reputation: 47
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...
 
Old 02-22-2007, 05:26 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 02-22-2007, 05:45 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 64
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.

Last edited by matthewg42; 02-22-2007 at 05:47 AM.
 
  


Reply

Tags
help, please


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Which signals can and should be caught? introuble Programming 2 08-21-2006 06:52 AM
Caught in install loop Trio3b Slackware 6 08-22-2005 06:04 AM
Caught in the LOOP Nimoy Linux - General 11 02-01-2004 03:57 AM
XFree86 4.3.0 : Caught signal 11 Enforcer *BSD 4 03-31-2003 02:44 PM
Signal 11 Caught ixion Linux - General 0 02-11-2003 06:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration