I'm trying to write a program that gives a table of temperature for each hour in one day with some starts that give an idea about the temperature. My problem is that eclipse (I use it on Mac OS) gives me "method 'open' can not be resolved". Would you please help me solving this problem?
And it would be so great if you have any suggestions in order to better the program.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include<cstring>
using namespace std;
int main()
{char temp[30];
cout<<"what is the name of the file you are using? ";
int rain[24],i,j,stars;
cin>>temp;
ifstream input;
input.open(temp);
if(input.fail())
{ cout<<"file did not open please check it\n";
system("pause");
return 1;
}
for(i=0;i<24;i++)
{input>>rain[i] ;
}
for(i=-30;i<=120;i+=30)
cout<<setw(10)<<right<<i;
cout<<endl;
for(i=0;i<24;i++)
{cout<<setw(10)<<left<<rain[i];
if(rain[i]<0)
{stars=(int)(abs(rain[i]) /3.);
for(j=1;j<10-stars;j++)
cout<<" ";
for(j=0;j< stars;j++)
cout<<"*";
cout<<"|\n";
}
else
{for(j=1;j<10;j++)
cout<<" ";
cout<<"|";
stars=(int)(rain[i]/3.);
for(j=0;j<stars;j++)
cout<<"*";
cout<<endl;
}
}
input.close();
system("pause");
return 0;
}
This should work..
Open Project explorer, right click on your project > Index > Rebuild
Related
I have some question about file I/O in c++.
When I use while(fin>>x) for twice in my program and cout two times, only the first time will display on my screen.
And my test.txt is:
I like eat banana
I like eat apple
My code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
int main(){
ifstream fin;
fin.open("test.txt");
if(fin.fail()){
cout<<"Error!"<<endl;
exit(1);
}
else{
int i=0,j=0;
string x,y,a[20],b[20];
while(fin>>x){
a[i]=x;
i++;
}
fin.
while(fin>>y){
b[j]=y;
j++;
}
for(int q=0;q<20;q++){
cout<<a[q]<<" ";
}
for(int w=0;w<20;w++){
cout<<b[w]<<" ";
}
}
fin.close();
return 0;
}
The reason making fin >> x return false (and exiting the first loop) still exists when you write fin >> y right afterwards. Hence, once fin >> x-loop is left, fin >> y-loop will not be entered.
So i got a little beginner problem here.
I cant seem to print out a string with char and integers in it.
#include <iostream.h>
#include <conio.h>
Main()
{
Char date[20];
Clrscr();
Cout<<"enter date: ";
Cin>>date;
Cout<<endl;
Cout<<date;
Getch();
Return 0;
}
My input here is suppose to be :
January 1-5,1999
But all it shows is :
January.**
Use getline(). Otherwise it cuts it after a space. Also, do not use capitals for cout, etc.
Like
string date;
getline(cin,date, '\n');
The answer by #Caspar Wylie is correct but if are using a very old/outdated compiler(guessed from conio.h and iostream.h header files) then try this
#include <iostream.h>
#include <conio.h>
int main()
{
char date[20];
cin.getline(str,20);
cout << date << endl;
getch();
return 0;
}
Yes, this is a question for a class, but I don't want my homework done for me. I just need to figure out where I'm going wrong. The question that I have to figure out states this:
Write a program which uses the file produced in Lab 1 as its input file. This program gets user input of a value from 1000 to 10000, and counts how many times the user's value appears in the input file. It reports to the user using screen output.
Here's the code that I have after several failed attempts:
#include <iostream>
#include <fstream>
using namespace std;
int count(int number, int input, int length)
{
int counter = 0;
for(int i = 0; i < length; i++)
if(input == number)
counter++;
return counter;
}
int main()
{
int num,input;
ifstream fin;
fin.open("ran_num.txt");
if(fin.fail())
{
cout<<"Input file opening failed."<<endl;
cin.get();
}
cout<<"Enter a number between 1000 to 10000:";
cin>>num;
fin>>input;
cout<<num<<"appears "<<count(num, input, 3000)<<" times in the file."<<endl;
return 0;
}
I'm completely lost and just need to figure out what I use to count a user inputer value. Any help is appreciated!
Edit
This is what I have now. My program can now read the whole file, but I still do not know how to count a user-inputted number.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream fin("ran_num.txt");
int num, user_input;
cout<<"Enter a number between 1000 to 10000:";
cin>>user_input;
while(fin>>num)
{
if(num == user_input)
{
cout<<count++;
}
}
return 0;
}
Are you sure your program is opening the file?
You should check if the file is opened correctly, you also should close the file after use.
Also you're outputting count before incrementing it.
I suggest changing your while loop this way:
if (fin.is_open())
{
while (fin >> num)
{
if (num == user_input)
{
++count;
}
}
fin.close();
cout << count;
}
else
{
cout << "Unable to open file";
}
This is the correct code that I have. It works perfectly now thank you!
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream fin("ran_num.txt");
int num, user_input, count;
cout<<"Please enter a number from 1000 to 10000:";
cin>>user_input;
if (fin.is_open())
{
while (fin >> num)
{
if (num == user_input)
{
++count;
}
}
fin.close();
cout<<user_input<<" occurs "<<count<<" times.";
}
else
{
cout<<"Unable to open file.";
}
fin.close();
return 0;
}
This is my code!
#include "stdafx.h"
#include<iostream>
#include<conio.h>
void main()
{
clrscr();
int no;
cout<<"Enter a number";
cin>>no;
getch();
}
and I get this error here!
I think I might have to download some extra visual studio c++ related directories, but still some suggestions please
clrscr() is not a standard function. Visual Studio does not have it. However, MSDN does document how to clear the screen using system("cls"), or FillConsoleOutputCharacter() and FillConsoleOutputAttribute().
As for the cin/cout errors, you need to prefix them with the std:: namespace qualifier, eg std::cin and std::cout, or use a separate using namespace std; statement in your code below the header #include statements.
Try this:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <conio.h>
void clrscr()
{
std::system("cls");
}
int main()
{
clrscr();
int no;
std::cout << "Enter a number";
std::cin >> no;
getch();
return 0;
}
Or:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
void clrscr()
{
std::system("cls");
}
int main()
{
clrscr();
int no;
cout << "Enter a number";
cin >> no;
getch();
return 0;
}
Right now your c++ program doesn't know what clrscr(); is..
you have to define that function. To define it, see #Remy Lebeau's answer.
One quick solution instead of creating a function to clear out the screen is to simply cout a bunch of blank spaces.
so in your main you can simply put :
std::cout << string(50, '\n');
So I wanted to add a stat() check on a dir. After compiling it started tossing some really strange stuff. Heres what I got:
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <boost/algorithm/string.hpp>
#include <sys/stat.h>
using namespace std;
using namespace boost;
struct stat sb;
int main()
{
cout<<" \n";
cout<<" Account Progam\n"
cout<<" \n";
cout<<" Created by: Illyduss\n";
cout<<" 2015\n";
cout<<" \n";
cout<<" \n";
cout<<" \n";
std::string account;
cout<<"Do you already have an account? \n";
cout<<"Type 'NEW' to create an account or enter your account name.\n";
cout<<"Account Name: ";
cin>> account;
cin.ignore();
string str1(account);
to_upper(account);
if (account == "NEW"){
...Some if checking here and junk...
...And then what was recently added the bugs my cin.get()...
else {
cout<< "Welcome back, '" << account << "'!\n";
struct stat sb;
if (stat("/home/user/Account Program/accounts",&sb) == 0 && S_ISDIR(sb.st_mode)){
cout<<"Please enter your password: ";
}
else {
cout<< "There is no account by that name, please type NEW to create a new account.\n";
}
}
}
cin.get();
}
I added cin.get() to keep the terminal open after it's run so I can see whats going on, but I believe it is conflicting with my dir check because it was running just fine until I added that check in to see if an account was already made before attempting to open the account dir.
Specifically I am getting these errors:
intro.cpp:112:2: error: ‘cin’ does not name a type
cin.get();
^
intro.cpp:113:1: error: expected declaration before ‘}’ token
}
^
user#computer:~/Account Program$ g++ intro.cpp
intro.cpp:112:2: error: ‘cin’ does not name a type
cin.get();
^
intro.cpp:113:1: error: expected declaration before ‘}’ token
}
^
As well it is tossing me an error on the } used to close out int main() like once it gets to the dir check it tosses me out of my int main()? Am I not closing off the stat() correctly?
The answer is:
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <boost/algorithm/string.hpp>
#include <sys/stat.h>
using namespace std;
using namespace boost;
struct stat sb;
int main()
{
cout<<" \n";
cout<<" Account Progam\n"
cout<<" \n";
cout<<" Created by: Illyduss\n";
cout<<" 2015\n";
cout<<" \n";
cout<<" \n";
cout<<" \n";
std::string account;
cout<<"Do you already have an account? \n";
cout<<"Type 'NEW' to create an account or enter your account name.\n";
cout<<"Account Name: ";
cin>> account;
cin.ignore();
string str1(account);
to_upper(account);
if (account == "NEW"){
...Some if checking here and junk...
...And then what was recently added the bugs my cin.get()...
else {
cout<< "Welcome back, '" << account << "'!\n";
struct stat sb;
if (stat(("/home/user/Account Program/accounts/"+account).c_str(),&sb) == 0 && S_ISDIR(sb.st_mode)){
cout<<"Please enter your password: ";
}
else {
cout<< "There is no account by that name, please type NEW to create a new account.\n";
}
}
cin.get();
}
I had an extra } after the last else and needed to add the child dir that was being checked via like so ("/home/user/Account Program/accounts/"+account).c_str().