im lost making a code that runs this. it has to include "while" command but im having difficulty with the counter vs the name =/ can someone help

Enter your friend’s name(stop to quit):john
Enter your friend’s name(stop to quit):megan
Enter your friend’s name(stop to quit):stop

Congratulations you have 2 friends!!!

(If user types stop in beginning reply)

Go make some you emotional loser

sorry i forgot to include the code i already have

#include<iostream>
using namespace std;


int main()

{
    char name;
    int counter;
    
    
    
while ((counter = name) || (counter = 1)  )
{
      cout<<"Enter your friend's name (""stop"" to quit): ";
    cin>>name;
      
      cout<<"so you have"<<counter <<"\n\n";
counter ++;
}
cout<<"Go Make some friends you emotional loser!\n";    
    
    
    
    
    
    
    
    
    
      system ("pause") ;
  return 0;
}

You have a few problems with your code.
I made some changes and got it to work.
First, I made name a string value instead of a char.
I also changed to while loop to:

while (name != "stop" )
{
      cout<<"Enter your friend's name (""stop"" to quit): ";
      cin>>name;
      counter++;
}

What this does is increment counter when a name is added.
This includes the name "stop" so when you output the total number of friends you must use counter-1.
Let me know if you have any questions about this.

I revided the code to make it more efficient.
This version includes if/else statements to output specific messages based on user input:

while (name != "stop" )
{
      cout<<"Enter your friend's name (""stop"" to quit): ";
      cin>>name;
      if(name != "stop")
      counter++;
}
if (counter>0)
cout<<"so you have "<<counter <<" friends"<<endl;

else
cout<<"go make some friends loser"<<endl;

As you can see this new code outputs counter NOT counter-1 because I have included it in an if statement. This code is much better than my first attempt.
Let me know if you have any questions about this code.

It is possible without using a string. The coding will not be that hard.

First of all. Char is just a single character. To put more than 1 character in this variable, you need to write:

char name [10];

Keep in mind that you need to put the length of the array (I used 10). If you do not, then most likely your program will crash. A bit advanced, but when an char array is lenght 10, you can add 9 chars (+ null terminator) . To make it a bit more clear, I'll show this:

char name [10] = "Example";
char name [10] = {'E', 'x', 'a', 'm', 'p', 'l', 'e', '\0'};

So this array has 7 + 1 initialized elements.

Oh, and another thing. You cannot compare a string that is not initialized yet. Well.... you sort of can, but name will have some random chars. I just don't recommend it. This was the case, because you didn't initialize name when you reached the while loop for the first time.

Well, since the one above me already gave the full code, I decide to give it too, because I know that otherwise you would ignore my post because you already have access to the full code. But this one is with a char array instead of string.

The only function that can be a bit confusing is strcmp(). This function compares 2 const char* (= strings). When they are equal, the result is 0. Otherwise it is non-zero.

#include<iostream>
using namespace std;

int main()

{
    char name[10]= ""; // empty string
    int counter = -1;
    
	while (strcmp(name, "stop") != 0)
	{
		cout<<"Enter your friend's name (""stop"" to quit): ";
		cin>>name;
		counter++;
	}
	if (counter == 0)
	{
		cout<<"Go Make some friends you emotional loser!\n";
	}
	else
	{
		cout << "Congratulations you have " << counter << " friends!!\n";
	}
	system("pause");
	return 0;
}

It is possible without using a string. The coding will not be that hard.

Don't you think using a string is easier than an array, or is that just me?:?:
Not that arrays are exactly rocket science. :)

I think it cannot hurt to know how a char array works. In my opinion it is something that you should know if you want to become better in C++ programming. And honestly I don't think that Mark knew how to use a char array.

But I agree. A string is easier to use than a char array. But I never said it would be easier with a char array.

#include<iostream>
using namespace std;


int main()

{
    char name;
    int counter;
    
    
    
while (name!="stop")
{
      cout<<"Enter your friend's name (""stop"" to quit): ";
    cin>>name;
      
      cout<<"so you have"<<counter <<"\n\n";
counter ++;
}
cout<<"Go Make some friends you emotional loser!\n";    
    
    
    
    
    
    
    
    
    
      system ("pause") ;
  return 0;
}

In my opinion the cleanest way to do this is just have the user return a blank line when they're done. So "while (name!='')" would do that.

I think it cannot hurt to know how a char array works. In my opinion it is something that you should know if you want to become better in C++ programming. And honestly I don't think that Mark knew how to use a char array.

But I agree. A string is easier to use than a char array. But I never said it would be easier with a char array.

He's probably in a class, its best not to get ahead of yourself, let him learn at his own pace.

#include<iostream>
using namespace std;

int main()

{
    char name[10]= ""; // empty string
    int counter = -1;
    
	while (strcmp(name, "stop") != 0)
	{
		cout<<"Enter your friend's name (""stop"" to quit): ";
		cin>>name;
		counter++;
	}
	if (counter == 0)
	{
		cout<<"Go Make some friends you emotional loser!\n";
	}
	else
	{
		cout << "Congratulations you have " << counter << " friends!!\n";
	}
	system("pause");
	return 0;
}

My list of question :

1) WHY ?
2) What happens when a name is longer than 9 characters ?
3) Is it easier ?
4) Less code ?
5) Better looking ?
6) more understandable ?
7) Less prone to bugs ?

Additional details :

It needs to use <cstring> library for it to be able to use strcmp(...).

This code :

if(counter == 0)

is the same as

if(!counter);

So, there is no need to use the boolean compare operator when
a "not" can do the job.

2) That was not the question. You should read the question. Mark said: "im lost making a code that runs this." You see that? He said THIS. Not that it should hold for all names.
1, 3-7) Those questions are already answered or based on nothing besides being annoying.

And no. I didnt have to include <cstring>. The code compiled and the program worked fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.