FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

error compiling example

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
Rohan



Joined: 05 Jan 2007
Posts: 22

PostPosted: Mon Feb 05, 2007 12:23 pm    Post subject: error compiling example Reply with quote

Code:
import std.cstream; /* for din.readLine() */
import std.stdio;   /* for writef()/writefln() */
 
int main()
{
  char[] name;

  writef("Hello friend. Please enter your name: ");
  name = din.readLine();

  writefln("Thanks, %s",name);
}


When I compiling it in Elephant I have got error "Function test.main expected to return a value of type int" who can explain this error?
Back to top
View user's profile Send private message
Derek Parnell



Joined: 22 Apr 2004
Posts: 408
Location: Melbourne, Australia

PostPosted: Mon Feb 05, 2007 1:12 pm    Post subject: Re: error compiling example Reply with quote

Rohan wrote:
Code:
import std.cstream; /* for din.readLine() */
import std.stdio;   /* for writef()/writefln() */
 
int main()
{
  char[] name;

  writef("Hello friend. Please enter your name: ");
  name = din.readLine();

  writefln("Thanks, %s",name);
}


When I compiling it in Elephant I have got error "Function test.main expected to return a value of type int" who can explain this error?


You coded : int main()

Which means that you wanted main() to return an int value but you do not have a return statement coded.

Either insert a sreturn statement or change it to: void main()
_________________
--
Derek
skype name: derek.j.parnell
Back to top
View user's profile Send private message
Rohan



Joined: 05 Jan 2007
Posts: 22

PostPosted: Mon Feb 05, 2007 1:29 pm    Post subject: Reply with quote

That mean bug in tutorial?
http://en.wikibooks.org/wiki/A_Beginner%27s_Guide_to_D/The_Basics/Basic_Input
Back to top
View user's profile Send private message
jcc7



Joined: 22 Feb 2004
Posts: 657
Location: Muskogee, OK, USA

PostPosted: Mon Feb 05, 2007 3:10 pm    Post subject: Reply with quote

Does this example work better for you? StdInStdOutStreamsExample
Back to top
View user's profile Send private message AIM Address
Rohan



Joined: 05 Jan 2007
Posts: 22

PostPosted: Wed Feb 07, 2007 10:25 am    Post subject: Reply with quote

Thanks! Where I can read about format string spec in D?
When need %lf and when other...
Back to top
View user's profile Send private message
jcc7



Joined: 22 Feb 2004
Posts: 657
Location: Muskogee, OK, USA

PostPosted: Wed Feb 07, 2007 2:33 pm    Post subject: Reply with quote

The formatting strings depend on which function you're using.

If you want to use writefln (in std.stdio) or doFormat (in std.format):
http://www.digitalmars.com/d/phobos/std_format.html#format-string

If you want to use printf (or C-style formatting):
http://www.prowiki.org/wiki4d/wiki.cgi?HowTo/printf

I'd recommend you use writefln or doFormat as you'll have to opportunity for friendlier runtime error messages (printf will only tell you "Access Violation").
Back to top
View user's profile Send private message AIM Address
Rohan



Joined: 05 Jan 2007
Posts: 22

PostPosted: Sat Apr 07, 2007 8:09 am    Post subject: Reply with quote

Code:
import std.stdio;
import std.c.stdio;

int main() {
    char x, y, q;
    writef("Please enter value for x: ");
    scanf("%s", & x);
    writef("And for y: ");
    scanf("%s", & y);
    q=x*y;
    writefln("result: %s",q);
    return 0;

}

If for example I enter value "a" and "d" I have got error
result: Error: 4invalid UTF-8 sequence
Back to top
View user's profile Send private message
Rohan



Joined: 05 Jan 2007
Posts: 22

PostPosted: Sun Apr 08, 2007 3:57 am    Post subject: Reply with quote

And new question
Code:

import std.stdio;
void main() {
    char[] s;

    scanf("%s", & s);
    writefln("%s", s);

}

I've got error "Error: Access Violation"
Back to top
View user's profile Send private message
Bradley Smith



Joined: 20 Jun 2006
Posts: 60

PostPosted: Sun Apr 08, 2007 3:42 pm    Post subject: Reply with quote

The C library scanf is used because std.stdio contains the following:

Code:
public import std.c.stdio;



Therefore, a char* must be passed to scanf. For example,

Code:
import std.stdio;
import std.c.string;

void main(char[][] args)
{
   char[1024] buffer;
   scanf("%s", buffer.ptr);
   char[] input = buffer[0 .. strlen(buffer.ptr)];
   writef("%s", input);
}
Back to top
View user's profile Send private message
Rohan



Joined: 05 Jan 2007
Posts: 22

PostPosted: Sat May 05, 2007 5:42 am    Post subject: Reply with quote

How I can write this C string in D?
Code:
count << (char) i;
Back to top
View user's profile Send private message
jcc7



Joined: 22 Feb 2004
Posts: 657
Location: Muskogee, OK, USA

PostPosted: Thu May 10, 2007 12:41 pm    Post subject: Reply with quote

Rohan wrote:
How I can write this C string in D?
Code:
count << (char) i;
For those of us that don't know C/C++ (but do know some D), could you explain what you're trying to do?

Are you trying to take a char[] as input, and then store it as an integer or floating-point number?
Back to top
View user's profile Send private message AIM Address
davidb



Joined: 03 Apr 2007
Posts: 15

PostPosted: Fri May 11, 2007 3:24 pm    Post subject: Reply with quote

@Rohan:
Because your previous posts were console i/o related, I assume you meant "cout" instead of "count"?
(And I assume "i" is an int?)
Then you can use
Code:

   int i = 65;
   writefln(cast (char) i);

which prints 'A' to the console.

@jcc7:
Code:

   cout << whatever;

passes "whatever" to the standard Console OUTput stream ("cin >> whatever" is the equivalent,
reads from Standard INput stream and writes the data to "whatever"). "(char)" is an explicit cast,
"cast (char)" in D (although I don't think that was the part you asked after).

david
Back to top
View user's profile Send private message
Rommie



Joined: 23 Aug 2007
Posts: 6

PostPosted: Thu Aug 23, 2007 3:55 pm    Post subject: Reply with quote

Rohan wrote:
How I can write this C string in D?
Code:
cout << (char) i;

writef(cast(char)i);

Rommie
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group