4 Replies - 343 Views - Last Post: 31 July 2013 - 05:40 PM Rate Topic: -----

#1 sinha2366  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 31-July 13

Segfault Error

Posted 31 July 2013 - 05:03 PM

Hello,
My code is producing a "segmentation fault" error (when compiled).
I am a beginner and don't know what this means or how to fix it.

Here is the source code:

#include <iostream>
#include <cstring>
#include <cctype>

using namespace std;

void takeLine(char sentence[]);

int main()
{
  char sentence[70]; 
  cout << "Enter a sentence. ";
    cin.getline(sentence, 70);
  takeLine(sentence);  
  
  cin.get();
  return 0;
}

 void takeLine(char sentence[])
{
    int i = 0;
    char *p;
    
        sentence[i] = toupper(sentence[i]); // converts first letter to upper case
        i++;
    for (i < (int)strlen(sentence); i++;)/>/>  // converts all the letters after the first one to lower case
        sentence[i] = tolower(sentence[i]);
    
    p = strtok(sentence, ("' .'")); // sloppy but does the trick 
    
    while (p != NULL )
        {        
            cout << p; 
            p = strtok(NULL, ("' .'"));
                
            if (p == NULL)
                cout << ".";
            else
                cout << " ";
    }
}



Here is what the error looks like (specifically):

sen.cpp: In function `void takeLine(char*)':
sen.cpp:27: warning: statement has no effect

Thanks

This post has been edited by macosxnerd101: 31 July 2013 - 05:27 PM
Reason for edit:: Please use a descriptive title.


Is This A Good Question/Topic? 0
  • +

Replies To: Segfault Error

#2 michael072  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 103
  • Joined: 08-February 11

Re: Segfault Error

Posted 31 July 2013 - 05:31 PM

for (i < (int)strlen(sentence); i++;)/>


This is your problem. The syntax of a for loop is this for([initialization]; [condition]; [increment]) because of your location of semi colons you're saying this for([condition]; [increment]; [empty]). So you want the initialization part of your for loop to be empty since you declare your variable before the for loop.

for (;i < (int)strlen(sentence); i++)

This post has been edited by michael072: 31 July 2013 - 05:32 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3817
  • View blog
  • Posts: 13,531
  • Joined: 12-December 12

Re: Segfault Error

Posted 31 July 2013 - 05:31 PM

Your if statement is incorrect. This

for (i < (int)strlen(sentence); i++;)/> 

should be

for (;i < (int)strlen(sentence); i++)

This post has been edited by andrewsw: 31 July 2013 - 05:34 PM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Segfault Error

Posted 31 July 2013 - 05:36 PM

Your for statement is malformed.
for (i < (int)strlen(sentence); i++; )


The for statement consists of three sections, the initialization section, the condition section, and the increment section.

Your for statement has the condition section in place of the initialization section, and the increment section in place of the condition section, and no increment section.

You may want to review the following tutorial: Control Structures.

I also suggest you start using std::string instead of the C-strings. It'll make your job much easier.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 sinha2366  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 31-July 13

Re: Segfault Error

Posted 31 July 2013 - 05:40 PM

Oh my god!

Thanks a lot guys. I seriously don't know how that got in there!
I chekced and then double checked; I thought I fixed that part previously but it probably didn't save.

Thanks once again! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1