How to fix this?

This is a discussion on How to fix this? within the C++ Programming forums, part of the General Programming Boards category; Code: int Y; char ett[10], out[50]; cin>>ett; Y = atoi(ett); atoi() Makes Y the string ett for(int x = 0; ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    10

    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.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    Does the first one work if you change it to:

    Code:
    for(int x = 0; x < Y;x = x + 1){out[x] = 'I';}

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    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.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Here is the FAQ for getting a string from the user. It will explain what King Mir is talking about wrt limiting input.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    Thanks for the help, I did not know ' was different from "

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    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.
    Last edited by Matticus; 07-05-2011 at 10:09 PM.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    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.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    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.
    Last edited by Matticus; 07-05-2011 at 11:26 PM.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    giving d a value worked, thank you. Before i did that, the value for entering 1 was 2147348481, after, it gave the right answer.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    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.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by AndrewHunter View Post
    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.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    What is going on? The FAQ has been linked and everyone (ETA: excluding laserlight) is still writing obviously dangerous code.

    The FAQ says
    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.
    Last edited by whiteflags; 07-06-2011 at 12:04 AM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    Their poking at the use of cin>> rather then the use of getline()

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21