How to fix this?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-05-2011
Robertjh12
How to fix this?
Code:

```int Y; char ett[10], out[50]; cin>>ett; Y = atoi(ett); atoi() Makes Y the string ett for(int x = 0; x < Y;x = x + 1){out[x] = "I";}```
the error message is "invalid conversion from 'const char*' to 'char'"

Other way I tried this was
Code:

```int Y; string I; I = "I"; char ett[10], out[50]; cin>>ett; Y = atoi(ett); for(int x = 0; x < Y;x = x + 1){out[x] = I;}```
" cannot convert 'std::string' to 'char' in assignment"

Can someone help me fix this, or change it to a way that it will work? The idea is to convert the number into the string so that, if I input 5, the result is IIIII.
• 07-05-2011
Matticus
Does the first one work if you change it to:

Code:

`for(int x = 0; x < Y;x = x + 1){out[x] = 'I';}`
• 07-05-2011
King Mir
Code:

```int Y; char ett[10], out[50]; cin>>ett; Y = atoi(ett); atoi() Makes Y the string ett for(int x = 0; x < Y;x = x + 1){out[x] = 'I';}```
Also, you need to limit the number of character read from cin.
• 07-05-2011
AndrewHunter
Here is the FAQ for getting a string from the user. It will explain what King Mir is talking about wrt limiting input.
• 07-05-2011
Robertjh12
Thanks for the help, I did not know ' was different from "
• 07-05-2011
Matticus
Yes, 'I' denotes a single character, "I" denotes a string constant (const char*).

In your case, "out[50]" is a character array, so "out[n]" expects a single character.

EDIT: If you wanted to turn that character array into a string, you should read up a little on C-style strings if you haven't already.
• 07-05-2011
Robertjh12
I understand strings/chars, just not all the ways to use them. I am trying to use chars to do the roman numerals problem, because as an array, chars would be a bit easier to assign the letters to(at least in my view) and because they are what I understand a little better. I am not doing this as homework, or to complete the problem, just to give myself examples I can understand and reread for myself later, and to give myself ways around errors I may have. However, I keep getting errors, and I am not sure how to use getline in this, or whether it would make a difference.

Code:

```int main(){ int y, x, C,d; char ett[10], out[50]; cin>>ett; y = strlen(ett); //length of string ett C = atoi(ett);//making the inter the value in the string for(x = 0; x < C;x = x + 1){out[x] = 'I';}//makes each array digit = I for the number size. for(x = 0; x < 50;x = x + 1){if (out[x] == 'I'){d = d + 1;}}// SHOULD make int d change + 1 based on the amount of 'I' in string out cout<<out<<"  "<<d; }```
if input is 2, output should be "II 2". The output for the first is always right(although throughout doing this, it has also had at the end extra symbols) but the value for the second will not work.
• 07-05-2011
Matticus
I noticed you're not initializing 'd'. Perhaps you should initialize it with d = 0;

Code:

```#include <iostream> #include <strings.h> #include <stdlib.h> using namespace std; int main() {   int y, x, C,d = 0;   char ett[10], out[50];   cin>>ett;   y = strlen(ett); //length of string ett   C = atoi(ett);//making the inter the value in the string   for(x = 0; x < C;x = x + 1) {     out[x] = 'I';   }//makes each array digit = I for the number size.   for(x = 0; x < 50;x = x + 1) {     if (out[x] == 'I') {       d = d + 1;     }   }// SHOULD make int d change + 1 based on the amount of 'I' in string out cout<<out<<"  "<<d; }```
And I'd recommend using more whitespace (as above) to make the code easier to read.
• 07-05-2011
Robertjh12
giving d a value worked, thank you. Before i did that, the value for entering 1 was 2147348481, after, it gave the right answer.
• 07-05-2011
Matticus
You're welcome. The reason for this is because without initializing 'd', there was an unknown value in that memory location.

I modified the original program (without the initialization) to print out 'd' by itself before your loops. This is what I got:

Code:

```// OUTPUT: 2                  // input value 2130567168          // value of 'd' before initialization II  2130567170          // value of 'd' before initialization plus 2```
This is a very simple yet important mistake to avoid.
• 07-05-2011
laserlight
With respect to standard C and C++, <strings.h> is non-standard. You are actually looking to #include <cstring> instead. However, you can actually simplify to:
Code:

```#include <iostream> using namespace std; int main() {     int length;     cin >> length;     char out[50] = "";     if (length >= sizeof(out)) {         length = sizeof(out) - 1;     }     for (int i = 0; i < length; ++i) {         out[i] = 'I';     }     cout << out << "  " << length << endl; }```
• 07-05-2011
King Mir
Quote:

Originally Posted by AndrewHunter
Here is the FAQ for getting a string from the user. It will explain what King Mir is talking about wrt limiting input.

It's not necessary to use getline(). it is necessary to either specify the field width or use std::string.
• 07-05-2011
whiteflags
What is going on? The FAQ has been linked and everyone (ETA: excluding laserlight) is still writing obviously dangerous code.

The FAQ says
Quote:

There are many ways to get a string from the user in C++, however the two most common use C strings and C++ strings with the getline function.

C string:

Code:

```#include <iostream> int main() {   char line[256];   std::cout<<"Enter a string: ";   if ( std::cin.getline ( line, sizeof line ) )     std::cout<<"You entered \""<< line <<"\""<<std::endl; }```

I highly recommend doing what the FAQ says. Don't miss the link again.
• 07-06-2011
laserlight
Quote:

Originally Posted by whiteflags
What is going on? The FAQ has been linked and everyone is still writing obviously dangerous code.

What's so dangerous about my example code that is addressed by that FAQ?
• 07-06-2011
Robertjh12
Their poking at the use of cin>> rather then the use of getline()
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last