where's the mistake?

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

where's the mistake?

Postby Totti10 » Mon Sep 29, 2003 5:30 am

hi,

can anyone tell me why this does not compile?

Code: Select all
#include <stdio.h>
#pragma argsused
#pragma hdrstop
#include<iostream>
#include<conio>
using namespace std;
int main(void)
{
    int i = 0;

    for(i = 3; i < 7; i++)
    {
    cout << "%c" << (char)i, i);
    }
    getch();
    return(0);
}
Totti10
 

Postby Zen » Mon Sep 29, 2003 5:44 am

Dont know about pragma, but try something like this maybe..

Code: Select all
#include <stdio.h>
#pragma argsused
#pragma hdrstop
#include<iostream>
#include<conio>
using namespace std;
int main(void)
{
    int i = 0;

    for(i = 3; i < 7; i++)
    {
    cout <<(char)i<<" - "<<i<<endl;
    }
    getch();
    return(0);
}
User avatar
Zen
 
Posts: 1088
Joined: Wed Sep 24, 2003 1:41 am
Location: Norway

Postby icujc » Mon Sep 29, 2003 5:47 am

#include<conio>

should be

#include<conio.h>
--

"Computers are like air conditioners - They can't do their job
properly if you open windows."
User avatar
icujc
 
Posts: 83
Joined: Mon Sep 22, 2003 3:41 pm
Location: Alabama

Re: where's the mistake?

Postby Jimbo » Mon Sep 29, 2003 7:45 am

Totti10 wrote:
Code: Select all
    cout << "%c" << (char)i, i);


its this line thats the problem. notice how you have one '(' and two ')'. also, you are mixing up cout with printf(). cout uses the '<<' operator to ouput variables to a string, but it lacks printf()'s capability to format any variables outputted.
User avatar
Jimbo
 
Posts: 601
Joined: Thu Sep 25, 2003 6:48 pm
Location: Seattle

Postby raimo » Mon Sep 29, 2003 8:00 am

You should decide whether to use C++ or C.
If C++, then replace <stdio.h> with <cstdio>, throw those #pragmas into rubbish can and learn how to use iostream classes.
std::cout << static_cast<char>(i); is probably what you want to say on that one line.
User avatar
raimo
 
Posts: 372
Joined: Fri Sep 26, 2003 6:50 am
Location: Finland

or you could simplify...

Postby c0de » Mon Oct 06, 2003 3:16 am

Code: Select all
#include <iostream>

using namespace std;

int main(void)
{
  int i = 0;
  for(int i = 0; i <= 6; i++)
    {
       cout << i << "\n";
    }
}


wouldn't that give you your desired output? I know it does not convert the int to a char but it still output 1 2 3 4 5 etc...
eh?
Link Less Think More
User avatar
c0de
 
Posts: 13
Joined: Tue Sep 30, 2003 11:30 pm


Return to For Beginners

Who is online

Users browsing this forum: No registered users and 2 guests