You are here:

C++/C++ newbie

Advertisement


Question
QUESTION: Hey, I'm a completely new to C++, and I've been having some problems with the programs I produce, can you take a look at them? Thanks.

1) why does the message "can not convert from 'const char [7]' to 'char'" occur when i try to run the following program:
#include <iostream>
using namespace std;

int main() {
  
  char pass = "joping";
  char enter;

  cout << "Enter the password: ";
  cin >> enter;

  if (pass == enter) cout << "document found.\n";
  else cout << "Access denied.\n";

  return 0; }

this program asks the user to enter a password to match the password set by the programmer.


2) I was asked to write a program that finds all the prime number between 1 and 100, here is what I've wrote:
#include <iostream>
using namespace std;
int main() {
  int x, y;

  for (x=1,y=1; x >= y; x++,y++) {
     if (!((x % y) ==0)) cout << x << "\t"; }

  return 0; }

unfortunately, it didn't work. Can you give me some hint about how to write this program? It's okay if you can't help with homework questions.

3) this program asks to find the number of uppercase letters in a string that the user types in, and display the number; here is what I did:

#include <iostream>
#include <cstdio>
using namespace std;
int main() {
  char str[100];
  int i, count, h;

  count = 0;

  cout << "Enter the sentence here: ";
  gets (str);

  for (i = 0; i <= 100; i++) {
     if (isupper (str[i])) h = count + 1;
     else h = 0;
  }

  cout << "The number of uppercase character is: " << h;

  return 0; }

I don't understand why it didn't work, please explain.


4) the last program I will ask about is this:
#include <iostream>
using namespace std;
int main() {  
  int num [10];
  int i, u;

  for (u = 0; u < 10; u++) {
     cout << "Enter number: ";
     cin >> num [u]; }

  for (u = 0; u < 10; u++) {
     for (i = 9; i >= 0; i--) {
        for ( ;u != i; ) {
        if (num[u] = num [i]) cout << num [u] << " is duplicated.\n";
        else cout << "No duplicated values found.\n"; }
  }
  }

  return 0;}

the program is designed to find duplicated values for 10 values entered, I don't understand why it didn't work as it should. Can you tell me why?

Thank you so much,
Angela

ANSWER: Hello Angela, thank you for the questions.

1) The reason this generates a compile error is because you are trying to assign multiple characters to a variable type (char) that only stores a single character. If you want to assign multiple characters, you need to use a string:

std::string pass = "joping";

Also, you won't be able to input multiple characters into a char variable on your cout line. You'd have to use a string for that also.

std::string enter;
cin >> enter;
if(enter == pass)
{
// they match
}

2) I'm not sure if there is some sort of "way" of printing prime numbers that I'm forgetting, but here's how I would do it. A prime number is only divisible by 1 and itself right? So if you start a for loop at 2 and loop it until it reaches the value of the number and use the % operator like you were, that should work.

int main()
{
   cout << "1 "; // 1 is prime, so we can skip it

   for(int i = 2; i <= 100; i++)
   {
       bool isPrime = false;
       for(int j = 2; j < i; j++)
       {
         if((i % j) != 0)
         {
         isPrime = true;
         }
         else
         {
         isPrime = false;
         break;
         }
       }
       if(isPrime)
         cout << i << " ";
   }

   return 0;
}

Please note that All Experts doesn't preserve indentation unfortunately.

3) I think this doesn't work because your count variable is not initialized. It's value is undefined.

Try this:

int totalUpper = 0;

for (i = 0; i <= 100; i++)
{
if (isupper (str[i]))
totalUpper++;
}

That should count them correctly.

4) I'm not entirely sure what your program is supposed to do based on your description. Can you clarify?

However, one reason it doesn't work is because you are performing assignment in your if check instead of comparing the values:

if(num[u] = num[i]) assigns the value of num[i] to num[u]. Because of how C++ this works, this simply assigns the value, then the if check executes as long as num[u] isn't 0.

To compare the values it should be if(num[u] == num[i]).

I hope this information was helpful.

- Eddie

---------- FOLLOW-UP ----------

QUESTION: I should clarify,

1) I rewrite the program like this:
int main() {
  
  char pass[] = "joping";
  char enter[10];

  cout << "Enter the password: ";
  cin >> enter;

  if (pass == enter) cout << "document found.\n";
  else cout << "Access denied.\n";

  return 0; }
because error occurs when using the string code. However, even when I enter joping as password, it occurs to be wrong, can you tell me why?

2) I'm trying to write the prime program without using the isPrime function, can you give me some hints?

3) I tried to change it your way, like this:
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
  char str[100];
  int i, count = 0;

  cout << "Enter the sentence here: ";
  gets (str);

  for (i = 0; i <= 100; i++) {
     if (isupper (str[i])) count++;
     else count = 0;
  }

  cout << "The number of uppercase character is: " << count;

  return 0; }

but an error message says debug assertion failed when I try to run, please help.

Note: I'm using Microsoft Visual Basic C++ 2008 express edition.

Answer
Hello Angela, thanks for the question.

To use the std::string object, you must include <string> at the top of your file.

The reason comparing the char arrays doesn't work is because when you do == on char arrays, it compares the actual pointer value instead of the string.

If you want to compare using char arrays instead of the string object, there is a method called strcmp that takes 2 char arrays and returns 0 if they are equal:

char pass = "joping";
char enter;

cout << "Enter the password: ";
cin >> enter;

if(strcmp(pass, enter) == 0)
{
cout << "document found";
}

However, if you use the string class, then you can simply use == to see if they are equal.

I hope this helps.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.