-
linked list/ stored data
i trying to read a line of users input and store that information. My problem is that when the data is being read it has a problem with the spaces.
Code:
struct link_list
{
char info[100];
struct link_list *next;
struct link_list *pre;
};
typedef struct link_list link;
int count=0;
class ll
{
link *first,*list,*temp,*pointer;
public:
ll()
{
first=list;
}
int isempty(void);
void creat(void);
void append(void);
void del(void);
void insbeg(void);
void ins(void);
void display(void);
void inv(void);
void find(char x[100]);
};
int ll::isempty()
{
if(first==list)
return(1);
return(0);
}
void ll::creat()
{
if(first==list)
count=0;
else
count=1;
if(count!=0)
{
cout << endl <<"THE LIST ALREADY EXISTS!";
return;
}
char ch;
char tmp[100];
list=new link;
first=list;
while(1)
{
cout<<"Enter Info:";
// cin >> list->info;
cout<<"Do you want to continue:(y/n)";
cin >> ch;
if(ch=='n')
break;
list->next=new link;
list->next->pre=list;
list=list->next;
}
list->next=first;
first->pre=list;
}
ive tried to use a buffer (tmp[100]) to store the data like this..
Code:
cout<<"Enter Info:";
cin >> tmp;
**now how can i get tmp into list->info**
-
Search the board for getline...
-
as in
Code:
cin.getline(tmp,100,'\n');
?
or can i bypass the buffer and go with
Code:
cin.getline(list->info,100,'\n');
-
-
I know cin.getline works when you need to input stuff.
So it recognises the white spaces.
cin.getline(array,81);
if(array[a]==' ');
{
cout<<"You have found a white space"<<endl;
}
However, I've searched the web and the posts and I can't find stuff that to recognise white spaces or \n when I use a fstream and file pointers. I'm surprised at how elusive this is. Isn't there just a heading you can include or someting so it doesn't ignore white spaces. So you would have:
Code:
fstream file_pointer;
file_pointer.open("test.txt",ios::in);
char array[81];
do
{
file_pointer>>array;
if(array=='\n')
{
cout<<"newline was found";
}
if (array== ' ')
{
cout<<"a space was found";
}
..........
:(
-
You can use getline() with an ifstream object you declare just like you do with the standard istream object cin.
-
Consider this:
Code:
#include<iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
char buf[81];
char * p;
ifstream in("test.txt");
while(in.getline(buf,81))
{
cout<<buf<<endl;
}
p = strchr(buf, ' ');
if ( p != NULL)
{
cout<<"space found";
}
return 0;
}
Maybe not representative example but it's only one way to do it...
-
Code:
int main()
{
char buf[81];
char * p;
ifstream in("test.txt");
while(in.getline(buf,81))
{
cout<<buf<<endl;
}
p = strchr(buf, ' ');
if ( p != NULL)
{
cout<<"space found";
}
return 0;
}
I tried this but it still doesn't recognise white spaces?
Josh do u have any code??
-
Do you mean that p==NULL in that example? Are you still seeing the contents of the file printed to the screen?
The more specifically you tell us your problems, the easier it us for us to help.
-
Ups, my mistake
After while loop end of course there is no point to seek for characters
Code:
int main()
{
char buf[81];
char * p;
ifstream in("test.txt");
while(in.getline(buf,81))
{
cout<<buf<<endl;
p = strchr(buf, ' ');
if ( p != NULL)
{
cout<<"space found";
}
}
return 0;
}
-
Try this:
Code:
cout << "enter a phrase with a space, a tab and a newline char." << endl;
cout << "end the input by entering an asterix, * and the enter key" << endl;
char input[81];
cin.getline(input, 80, '*');
/*now enter the following line using enter key and tab key instead of the \n and \t and complete the input after the * by pushing the enter key a second time.
Hi.\nThis is \t a test.*
*/
cout << "the characters entered were:" << endl;
int max;
int i;
max = strlen(input);
for( i = 0; i < max; ++i)
{
if(input[i] == ' ')
cout << "space";
else if(input[i] == '\t')
cout << "tab";
else if(input[i] == '\n')
cout << "new line char";
else
cout << input[i];
cout << ' ';
}
-
isspace() returns true if the character passed to it is ' ', a tab, a newline, etc. Just remember to #include <cctype>
-
And to be precise:
The standard white-space characters are the following: space, tab, carriage-return, newline, vertical tab, and form-feed. In the C locale,
isspace() returns true only for the standard white-space characters.
But notify that the behavior of the isspace() function is affected by the current locale.
- Micko