|
-
February 27th, 2012, 06:11 PM
#1
[RESOLVED] Segmentation fault and can't figure out why
It seems to be going null. I can't get a null value, I want it to accept input till the user hits enter with no text typed. I've tried checking to see if the input is NULL, "", and "\n" all to no avail.
Code:
#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstring>
using namespace std;
int main()
{
string info = "";
cout << "Enter some info. \n";
int counter;
string newInfo = "";
counter = 0;
cout << counter;
bool noMore = false;
while(noMore == false)
{ // begin while
if (counter == 0)
{ // begin if
getline(cin, info);
cout << info.c_str();
if (info.c_str() == "")
{ // begin if
cout << "Went here.";
noMore = true;
} // end if
else
counter++;
} // end if
else
{ // begin else
getline(cin, newInfo);
cout << newInfo.c_str();
if (newInfo.c_str() == "")
{ // begin if
noMore = true;
} // end if
else
{ // begin else
info = info + "\n" + newInfo;
counter++;
} // end else
} // end else
} // end while
if (info.c_str() == "\n")
{
cout << "Abnormal termination.";
return 1;
}
/* the identifier for the shared memory segment */
int segment_id;
/* a pointer to the shared memory segment */
char *shared_memory;
/* the size (in bytes) of the shared memory segment */
const int size = 4096;
/* allocate a shared memory segment */
segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
/* attach the shared memory segment */
shared_memory = (char *) shmat(segment_id, NULL, 0);
/* write a message to the shared memory segment */
sprintf(shared_memory, info.c_str());
/* now print out the string from shared memory */
printf("*%s\n", shared_memory);
printf("*%s\n", segment_id);
/* now detach the shared memory segment */
shmdt(shared_memory);
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|