Results 1 to 5 of 5

Thread: conversion problem

  1. #1
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy conversion problem

    I do a turtle graphics program and having and a hell of time with it.
    Here is the code that gives me problems
    Qt Code:
    1. void checkpos(char cmd[3])
    2. {
    3. int count=0;
    4. int move[5];
    5. char tmp'
    6. for(int i=0;i<3;++i)
    7. if (cmd[i]==',')
    8. {
    9. tmp=cmd[i+1];
    10.  
    11. move[count]=atoi(tmp);
    12.  
    13. }
    14. }
    15.  
    16.  
    17. This code gets the number of moves in the command 5,x. I am getting this error:
    18.  
    19. H:/C++_How_To_Program/chap04/Turtle_graphics/main.cpp:95: error: invalid conversion from 'char' to 'const char*'
    20.  
    21. How can I convert the [B]cha[/B]r to an[B] int [/B]and get rid fo the error ?:confused:
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: conversion problem

    You have a error while declaring tmp, notice the ' instead of the ;
    and try
    Qt Code:
    1. move[count]=atoi(&tmp);
    To copy to clipboard, switch view to plain text mode 
    Last edited by john_god; 19th February 2010 at 19:16.
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: conversion problem

    while that fix might compile, it is wrong.

    try

    Qt Code:
    1. void checkpos(char cmd[3])
    2. {
    3. int count=0;
    4. int move[5]; // why an array? this code never can parse more than one...
    5. const char *tmp;
    6. for(int i=0;i<3;++i)
    7. if (cmd[i]==',')
    8. {
    9. tmp=&cmd[i+1];
    10. move[count++]=atoi(tmp);
    11. // and now? break? cmd can not contain further moves....
    12. // what about the (skipped) 'x'?
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    Note that char cmd[3] is quite a limitation and prevents you from parsing stuff like "15,x"

  4. #4
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: conversion problem

    I would be inclined to not worry about such low level processing and simply pass strings around. Something more like this:
    Qt Code:
    1. void simpleRegEx(const QString& s)
    2. {
    3. QRegExp regExp("(\\d+)\\s*,\\s*(\\S+)");
    4. if (regExp.indexIn(s) >= 0)
    5. {
    6. qDebug(qPrintable(QString("Number = %1").arg(regExp.cap(1).toLong())));
    7. qDebug(qPrintable(QString("Command = %1").arg(regExp.cap(2))));
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: conversion problem

    Thanks all. The problem has been solved.

Similar Threads

  1. Replies: 2
    Last Post: 20th October 2008, 18:34
  2. wchar to T50 conversion
    By rajveer in forum General Programming
    Replies: 1
    Last Post: 29th August 2008, 13:32
  3. RGBComponents / QImage Conversion problem
    By sincnarf in forum Qt Programming
    Replies: 2
    Last Post: 30th July 2007, 23:41
  4. Reg - Conversion of Qt3 to Qt4
    By suresh in forum Qt Programming
    Replies: 10
    Last Post: 28th August 2006, 23:10
  5. conversion of color
    By Stephano in forum Qt Programming
    Replies: 5
    Last Post: 22nd May 2006, 11:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.