Using setfill to populate a line of 35 *

I am trying to figure out how to get a line of 35 "*" using the setfill command. I can't seem to get it to run without having more than just "*" in the line. I have tried several variations but I can't get it to run. I think I have the simplest code set up that I can with the most straight forward direction to get it done.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{

char ch;
ch="*";

cout << right <<setw(34)<< setfill(“*”)<< "*" << endl;
return 0;

}


I have tried several variations of this. I am obviously not understanding something but, I don't know what. Any help would be greatly appreciated.
First of all, how is your code even compiling?

8
9
char ch;
ch="*";

Here, "*" is a string, not a character -- so this shouldn't have compiled.
If you want a character, you have to use '*'.

cout << right <<setw(34)<< setfill(“*”)<< "*" << endl;
Those weird “ ” symbols should have also given you an error.
In any case, for a character, it should be '*' (single quotes).

This works for me:
1
2
3
4
5
6
7
8
#include<iostream>
#include<iomanip>

int main()
{
    std::cout << std::setw(35) << std::setfill('*') << "" << std::endl;
    return 0;
}

The output is 35 *s, as expected.

You could also just use a loop:
1
2
for (int i = 0; i < 35; ++i)
    std::cout << '*';
I don't know? Maybe it is the version I am using, 2008. According to the text I was using the double quotes were what I was supposed to be using. It could be a misprint in the electronic book too. That has been known to happen. Probably more because I am greener than an evergreen conifer. Thanks for the input.

Hmmm..., that still doesn't work. I have tried modifying it several ways. Getting this line.

1>LINK : fatal error LNK1168: cannot open C:\Users\Thomas\Documents\Visual Studio 2008\Projects\runcheck\Debug\runcheck.exe for writing

Not sure.

Thanks for the help anyway. I appreciate it. I would try the loop but it is not a command we have learned yet. They want the setfill used.
Last edited on
Assuming you didn't do something to nuke the ACL's on that file this is the error you get when you are trying to compile and your application is still loaded into memory. Open task manager and kill it, if it isn't there and the problem is still occurring then Windows only thinks that the file is locked (you can test this by trying to delete the executable, at the very least it may give you a more helpful error) so rebooting your PC should fix that issue. If the problem STILL persists then temporarily disable your AV suite or add an exception for your working directory, it maybe that the AV thinks your executable is a virus.
Last edited on
Topic archived. No new replies allowed.