0
#include <iostream.h>

void main()
{
   char line[25];
   cout << " Type a line terminated by carriage return\n>";
   cin.get( line, 25 );
   cout << ' ' << line;
}

I am new to this forum and also C++
I want a help with cin.getline() function..

As you see the two codes above both use the getline() function.

The thing is that in the big code getline is not putting any string into "name".
when i choice is selected it just jumps to the next one. :(

can anyone suggest any solution and a reason for this.

n00b.

Attachments
#include<iostream.h>
#include<conio.h>
#include<process.h>

class base
{
	protected:
		int rollno;
		char name[20];
	public:
		void getdata()
		{
			cout<<"enter the roll no";
			cin>>rollno;
			cout<<"\n enter the name";
			cin.getline( name,20);
		}
		void putdata()
		{
			cout<<"\n Roll no.="<<rollno;
			cout<<"\n Name ="<<name;
		}
 };
 class marks : public base
 {
	protected:
		int m1,m2,total;
	public:
		void getmarks()
		{
			cout<<"\n enter two marks";
			cin>>m1>>m2;
		}

		void tmarks()
		{
			cout<<"\nThe two marks are:\n\t"<<m1<<"\n\t"<<m2<<"\n";
		}
 };

 class result :public marks
 {

 public:
	void display()
	{
		getdata();
		putdata();
		getmarks();
		tmarks();
		total=m1+m2;
		cout<<"The total marks are\n"<<total;
	}
	void putall(void)
	{
	putdata();
	tmarks();
	cout<<"The total marks are\n"<<total;
	}
	int getrollno()
	{
	return rollno;
	}
};

void main()
{
	int n=0,i=0,a=0,j=0;
	result r[15];
	while(1)
	{
	clrscr();
	cout<<"\n Enter yur choice";
	cout<<"\n1.Enter new student data";
	cout<<"\n2.Search a roll no";
	cout<<"\n3.Exit\n";
	cin>>n;

	  switch(n)
		{
		case 1:
		{
			r[i].display();
			getch();
			i++;
		break;
		}
		case 2:
		     { 	cout<<"\nEnter a roll no. ";
			  cin>>a;
			    for (j=0;j<i;j++)
				{
				if (a==r[j].getrollno())
				       {r[j].putall();
				       getch();
				       break;
				       }
				else
					continue;
				}
		     break;
		     }
		  case 3:
			 exit(0);
		}
	  }
}
4
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by p@rse
-1

Try this:

// DaniWeb Quicktest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h> //Use _getch() to pause the screen and not exit the program
#include <string>
using namespace std;

 
void main()
{
   char line[25];
   cout << " Type a line terminated by carriage return\n>";
   cin.getline( line, 25 ); //Your text was cin.get( line, 25 ) but modified to cin.getline( line, 25 )
   cout << line << endl;
   _getch(); //Taken from <conio.h> to pause program at end.
}

The Output should look like:

Type a line terminated by carriage return
>Hellooooo World!
Hellooooo World!

Hope this helped!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.