5 Replies - 1747 Views - Last Post: 17 October 2012 - 10:42 AM Rate Topic: -----

#1 sdc05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-October 12

Do while loop to a while loop (strings)

Posted 17 October 2012 - 08:55 AM

Hello,
I have tried multiple times to rewrite a given code in do while loop into a while loop and nothing seems to be working. When a rearrange most of the code I receive a debug assertion error upon execution. The purpose of the code is to read in a string and then output the string spelt backwards.
This is the code I have been given;
int main()
{
  string word;
  cout << "please enter a word: ";
  cin >> word;
  int length = word.length();
  int i = -1;
  do
    {
      i++;
char ch = word[i];
      word[i] = word[length-1];
      word[length-1] = ch;
    } while (i < --length);
    
cout << word;
    return 0;
    
}



This is the code I have now but when I put in a word the all the letters are coming out reversed but the last one stays in the same place.
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
  string word;
  cout << "please enter a word: ";
  cin >> word; 
	int length = word.length();
	int i = -1;

	
	for (int i = -1; i < --length; i++)
  {
	
	 char ch = word[i];
     word[i] = word[length-1];
     word[length-1] = ch;
	
	}
		cout << word;

    return 0;
    
}



Is This A Good Question/Topic? 0
  • +

Replies To: Do while loop to a while loop (strings)

#2 Coding in the name of  Icon User is offline

  • D.I.C Head

Reputation: 67
  • View blog
  • Posts: 232
  • Joined: 07-July 10

Re: Do while loop to a while loop (strings)

Posted 17 October 2012 - 09:01 AM

I believe that in your for loop i is being incremented after the compound statement is executed, but in your do while loop i is incremented first.
Was This Post Helpful? 0
  • +
  • -

#3 sdc05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-October 12

Re: Do while loop to a while loop (strings)

Posted 17 October 2012 - 09:20 AM

i just tried switching to

	for ( int i = -1; i++; i < --length)
  {
	
	 char ch = word[i];
     word[i] = word[length-1];
     word[length-1] = ch;
	
	}
		cout << word;



but now only my first and last letters switch places and the rest stay the same!
Was This Post Helpful? 0
  • +
  • -

#4 Coding in the name of  Icon User is offline

  • D.I.C Head

Reputation: 67
  • View blog
  • Posts: 232
  • Joined: 07-July 10

Re: Do while loop to a while loop (strings)

Posted 17 October 2012 - 09:33 AM

What you have now is completely wrong. Remember the format of for statements. It sounds like this assignment is designed to test your understanding of the difference between while statements and do-while statements, so you probably shouldn't be using a for statement at all.

This post has been edited by Coding in the name of: 17 October 2012 - 09:34 AM

Was This Post Helpful? 0
  • +
  • -

#5 sdc05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-October 12

Re: Do while loop to a while loop (strings)

Posted 17 October 2012 - 09:36 AM

we have to convert the original into a simple while and a for while loop. Getting the same output as the do while loop!
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4073
  • View blog
  • Posts: 12,560
  • Joined: 25-December 09

Re: Do while loop to a while loop (strings)

Posted 17 October 2012 - 10:42 AM

There is no such thing as a "for while" loop in C/C++, there are three basic loop constructs in C/C++ for(;; ), while(), and do/while. See this link: Control Structures.

In your first post you show a do/while() loop and an attempt at rewriting this as a for(;; ) loop. Normally in a for loop you only change one variable contained in your condition statement. Trying to change both is usually wrong. Also remember that in C/C++ arrays start at 0 and stop at size - 1, but an array index can never be less than zero. So in the following statement you have done both:
	for (int i = -1; i < --length; i++)
  {

In the above snippet you shouldn't change length in the condition section, and i should start at zero. More like:
	for (int i = 0; i < length; i++, length--)
  {

Note you can have multiple, comma separated initialization and increase sections.

Jim

This post has been edited by jimblumberg: 17 October 2012 - 10:43 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1