if else-if else/do while statement trouble

The program runs... improperly

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 6358 Views - Last Post: 25 April 2009 - 02:36 PM Rate Topic: -----

#1 deer dance  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 116
  • Joined: 21-April 09

if else-if else/do while statement trouble

Posted 24 April 2009 - 08:22 PM

I'm currently in development of a "custom command line, of sorts".

It's still a fairly simple code, just an if else-if else statement inside of a do-while loop.

The do-while loop works just fine, but each of the if statements is supposed to display some text, which they do, but the only display the first line of text with-in the do-while loop

/* mOS Beta v1.0
 * 
 * Developed By: Austin Hoffmaster [deer dance]
 *
 * Date Began: 22-4-09
 * Date Ended: NULL
 *
 */

#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	char x;
	do   
	{
	 cout << "Enter Command: ";
	 cin >> x;
	
	 if(x = 'HELP')
	 {
		 system("CLS");
		 cout << "\nThis is the help screen." << endl << endl;
	 }
	 else if(x = 'DIR')
	 {
		 system("CLS");
		 cout << "\nThis is the Directory Manager." << endl << endl;
	 }
	 else if(x = 'SYSINFO')
	 {
		  system("CLS");
		  cout << "\nThis is where the mOS system information will be displayed to you." << endl << endl;
	 }	
	}	while(x = true);   
	system("pause");
	getchar();
	return 0;
}



Everytime I run the program, the do-while loop runs, but regardless of what I input, it spits out this line of text:
cout << "\nThis is the help screen." << endl << endl;


If anyone knows the solution to this problem, your help is much appreciated.
Also, I'm still fairly new to C++, so if there's anything else wrong with my code, please let me know.

Thank you again.

Is This A Good Question/Topic? 0
  • +

Replies To: if else-if else/do while statement trouble

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,543
  • Joined: 23-August 08

Re: if else-if else/do while statement trouble

Posted 24 April 2009 - 08:31 PM

char x;

You want a string, not a char.
if(x = 'HELP')

You want == not =...always remember, = for assignment, == for comparison. What you're doing there is assigning to x. Also, you want "HELP" not 'HELP'...single quotes are only for single characters; double quotes are for strings.
Was This Post Helpful? 1
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: if else-if else/do while statement trouble

Posted 24 April 2009 - 08:34 PM

Your test to control the do-while() loop is this
while(x = true);

1 - '=' and '==' are not the same thing.
You are using the wrong one here.
Can you see why?

2 - You are trying (and getting it wrong) to check if 'x' has been set to 'true' but you never do anything to change 'x' from the original value so it will never change and never be equal to 'true'.

EDIT
JackOfAllTrades gave you a better answer while I was typing.

This post has been edited by janotte: 24 April 2009 - 08:36 PM

Was This Post Helpful? 1
  • +
  • -

#4 deer dance  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 116
  • Joined: 21-April 09

Re: if else-if else/do while statement trouble

Posted 24 April 2009 - 08:34 PM

Thank you, I'll make the changes and report back on the results.
Was This Post Helpful? 0
  • +
  • -

#5 arrechin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 11-March 09

Re: if else-if else/do while statement trouble

Posted 24 April 2009 - 08:38 PM

not only that. the problem that u have is that u need to declare it as an array of chars. if you do
char x = "whatever";

is an error.
you should declare it like this
 char x []= "whatever";


This post has been edited by arrechin: 24 April 2009 - 08:55 PM

Was This Post Helpful? 0
  • +
  • -

#6 deer dance  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 116
  • Joined: 21-April 09

Re: if else-if else/do while statement trouble

Posted 24 April 2009 - 08:42 PM

I've made the changes that JackOfAllAces recommended, but now the while statement get's an error

 while(x = true);



Here's the error:

could not convert `(&x)->std::basic_string<_CharT, _Traits, _Alloc>::operator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](1)' to `bool'



I tried changed the '=' to '==', but it made no difference.

Any more suggestions?

Now arrechin is telling me it does need to be char.

Which is it, a char or a string?
Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: if else-if else/do while statement trouble

Posted 24 April 2009 - 08:44 PM

View Postarrechin, on 24 Apr, 2009 - 07:38 PM, said:

not only that. the problem that u have is that u need to declare it as an array of strings. if you do
char x = "whatever";

is an error.
you should declare it like this
 char x []= "whatever";



Be careful of this.

It is bad information.

In C a string is an array of characters
In C++ a string is a string.

In no way do you want an "array of strings" of strings as arrechin has written.

Just ignore everything other than JackOfAllTrades posting so far.
Was This Post Helpful? 0
  • +
  • -

#8 arrechin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 11-March 09

Re: if else-if else/do while statement trouble

Posted 24 April 2009 - 08:45 PM

you could use any of those. if you choose to use a string you need to include the string library and dont declare it as an array. if u decide to use a char u declare it as an array. its ur call.
Was This Post Helpful? 0
  • +
  • -

#9 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: if else-if else/do while statement trouble

Posted 24 April 2009 - 08:45 PM

View Postdeer dance, on 24 Apr, 2009 - 07:42 PM, said:

I've made the changes that JackOfAllAces recommended, but now the while statement get's an error
Now arrechin is telling me it does need to be char.


1 - ignore arrechin

2 - show us your code as it is now.
Was This Post Helpful? 0
  • +
  • -

#10 deer dance  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 116
  • Joined: 21-April 09

Re: if else-if else/do while statement trouble

Posted 24 April 2009 - 08:47 PM

/* mOS Beta v1.0
 * 
 * Developed By: Austin Hoffmaster [deer dance]
 *
 * Date Began: 22-4-09
 * Date Ended: NULL
 *
 */

#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	string x;
	do   
	{
	 cout << "Enter Command: ";
	 cin >> x;
	
	 if(x == "HELP")
	 {
		 system("CLS");
		 cout << "\nThis is the help screen." << endl << endl;
	 }
	 else if(x == "DIR")
	 {
		 system("CLS");
		 cout << "\nThis is the Directory Manager." << endl << endl;
	 }
	 else if(x == "SYSINFO")
	 {
		  system("CLS");
		  cout << "\nThis is where the mOS system information will be displayed to you." << endl << endl;
	 }	
	}	while(x = true);   
	system("pause");
	getchar();
	return 0;
}



Do you see the problem?

This post has been edited by deer dance: 24 April 2009 - 08:48 PM

Was This Post Helpful? 0
  • +
  • -

#11 arrechin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 11-March 09

Re: if else-if else/do while statement trouble

Posted 24 April 2009 - 08:48 PM

View Postjanotte, on 24 Apr, 2009 - 07:44 PM, said:

View Postarrechin, on 24 Apr, 2009 - 07:38 PM, said:

not only that. the problem that u have is that u need to declare it as an array of strings. if you do
char x = "whatever";

is an error.
you should declare it like this
 char x []= "whatever";



Be careful of this.

It is bad information.

In C a string is an array of characters
In C++ a string is a string.

In no way do you want an "array of strings" of strings as arrechin has written.

Just ignore everything other than JackOfAllTrades posting so far.



i never saif to make an array of strings. did u read what i said?
he never declared it as a string;
Was This Post Helpful? 0
  • +
  • -

#12 deer dance  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 116
  • Joined: 21-April 09

Re: if else-if else/do while statement trouble

Posted 24 April 2009 - 08:49 PM

Now I did ;)
Was This Post Helpful? 0
  • +
  • -

#13 arrechin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 11-March 09

Re: if else-if else/do while statement trouble

Posted 24 April 2009 - 08:52 PM

lmao i realized that in my first reply i did say strings my bad i meant to say characters... lol

anyways i clarified it after.
Was This Post Helpful? 0
  • +
  • -

#14 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: if else-if else/do while statement trouble

Posted 24 April 2009 - 09:01 PM

View Postdeer dance, on 24 Apr, 2009 - 07:47 PM, said:

	}	while(x = true);   
}



Do you see the problem?


OK after our little excursion into error and bruised ego with arrechin let's try and get back on track.

We will go very slowly because things have been so muddled up till now.
Hopefully with less 'players' we can move this forward instead of in circles.

1 - This is a comparison not an assignment.
while(x = true);
Do you agree?
So which of '=' and '==' do you think you should use?

2 - "x" is a string variable.
You can't compare a string variable to a boolean value 'true'
What do you want to compare 'x' to?
Was This Post Helpful? 0
  • +
  • -

#15 arrechin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 11-March 09

Re: if else-if else/do while statement trouble

Posted 24 April 2009 - 09:13 PM

bruised ego??? lmao u funny son
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2