Menu

Dev c++ problem

maly
2007-07-03
2012-09-26
  • maly

    maly - 2007-07-03

    Hi to all
    I have a small piece of code. Looks fine, but nothig shows up on the screen, it should though. Could anybody tell me why is that? Is there aany possibility that samething wrong with Dev c++. If so,How may I fix it
    Thank you

    This is piece of code

    include<iostream>

    include<cstring>

    void swap(char& temporary1,char& temporary2);

    int main()
    {
    using namespace std;
    string word,temp;
    int end;
    cout<<"What is the word you would like to reverse: ";
    cin>>word;

    end=word.length();
    int start=0;
    while (start&lt;end)
    {
          swap(temp[start],temp[end]);
          start++;
          end--;
    
    }
    cout&lt;&lt;temp&lt;&lt;endl;
    system(&quot;pause&quot;);
    return 0;
    

    }

    void swap(char& temporary1,char& temporary2)
    {
    using namespace std;
    char temp=temporary2;
    temporary2=temporary1;
    temporary1=temp;

    }

     
    • Wayne Keen

      Wayne Keen - 2007-07-03

      By the way, please be careful with your thread titles.

      This was not a "Dev c++ problem", it was the result of code that was flawed.
      There's nothing criminal about that, my hacked up code is also flawed, but
      done in such a way as to show you something. But when you say what your thread
      title is, the message coming across is that you are blaming the tool.

      This is a bad idea from a question asking standpoint, and bad logic as well.
      I have been programming for over 30 years now, and I can count on one hand the
      number of times that the tool was at fault for something - the rest of the
      time it was just plain Jane Wayne stupidity....

      Wayne

       
    • Ultim

      Ultim - 2007-07-04

      People blaming the compiler for their own stupid shortsighted errors? I don't believe it.

       
    • Wayne Keen

      Wayne Keen - 2007-07-03

      Look into how you are using the variable "temp". It appears to be set locally to the function "swap", yet you turn around and use it in "main" like it was the same variable.
      It's not, just because it is declared in two places.

      Consider this hacked up code - not fit for turning in as homework by the way:

      include<iostream>

      include<cstring>

      using namespace std;

      void swap(char& temporary1,char& temporary2);

      int main()
      {
      string word,temp;
      int end;
      cout<<"What is the word you would like to reverse: ";
      cin>>word;

      end=word.length() - 1;
      int start=0;
      while (start<end)
      {
      swap(word[start],word[end]);
      start++;
      end--;

      }
      cout<<word<<endl;
      system("pause");
      return 0;
      }

      void swap(char& temporary1,char& temporary2)
      {
      char temp=temporary2;
      temporary2=temporary1;
      temporary1=temp;

      }

      How does this code work differently than yours, and why?

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.