Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have written down a code in C++ to make the words appear in array from a string input.
But it doesn't works due to overflow or something. Compiler doesn't show any error though.
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
char a[100];
gets(a);
char b[100][100];
int I=0;
for(int j=0;j<strlen(a);j++) //runs the loop for words
{
int k=0;
do
{
b[I][k]=a[j];
k++;
}
while(a[j]!=' ');
I++;
}
cout<<b[0][0];
return 0;
}
If you're going to use C strings, you need to add a null terminator at the end of each string
do {
b[I][k]=a[j];
k++;
} while(j+k<strlen(a) && a[j+k]!=' ');
b[I][k] = '\0';
As ppeterka noted, you also need to change your loop exit condition to avoid an infinite loop.
Note also that the repeated calls to strlen here and in your code are wasteful. You should consider calculating this once before your for loop instead.
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
char a[100];
gets(a);
char b[100][100];
int I=0;
int j =0;
while(j< 100) //runs the loop for words
{
int k=0;
do
{
b[I][k]=a[j+k];
k++;
} while(a[j+k]!=' ');
b[I][k+1] = '/0';
I++;
j= j+k;
}
cout<<b[0][0];
return 0;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
For example: insert 1 5 88 99 7 in a set if 1,5,88,99,7 is given as an input and then pressed Enter.
My code:
#include <bits/stdc++.h>
using namespace std;
set <char> num;
set <char> ::iterator i;
int main()
{
int a;
while(a=getchar())
{
if(a!='\n')
{
if(a!=',')
num.insert(a);
}
else
break;
}
for(i=num.begin(); i!=num.end(); i++)
cout<<*i<<endl;
}
The output I'm getting:
1
5
7
8
9
#include <set>
#include <string>
#include <sstream>
using namespace std;
int main() {
// cin >> s;
string s = "5,2,3,4,1",t = "";
stringstream ss(s);
set<int> s_;
while( getline(ss,t,',') )
s_.insert(stoi(t));
for(auto i : s_)
cout << i << " ";
}
Outputs: 1 2 3 4 5
1) To match your modified question (reading integers of any length) char by char, means you need to look at each and see if it is a digit. If it is, you need to keep a running value based on each additional digit you receive.
2) Your set needs to be of int, not char if you're going to save the actual integer values.
#include <iostream>
#include <set>
using namespace std;
set <int> num;
set <int> ::iterator i;
int main()
{
int a;
int full_num=0;
while(a=getchar())
{
if (isdigit(a))
{
full_num = full_num*10 + (a - '0');
}
else if (a==',' || a=='\n')
{
num.insert(full_num);
full_num = 0;
if (a=='\n') break;
}
}
for(i=num.begin(); i!=num.end(); i++)
cout<<*i<<endl;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I was debugging this, and debugger skipped the last 'if' even 'sum' was equal to 'n' and jump straight to 'else', I don't know why. Please help.
P/s: Can I use dynamic array to increase the mobility of my program?
#include <iostream>
#include <math.h>
using namespace std;
int exponent_of_10(); // set position for digits
int exponent_of_10(int a, int b){
for(int j = b; j>0;j--)
{
a *= 10;
}
return a;
}
main() //check if the number was palindromic
{
int n;
int a[6]={0,0,0,0,0,0};
int i = 0;
int temp;
int S;
cout<< "Input n (maximum of 6 digits): ";
cin>> n;
do
{
if(n<1)
{break;}
temp=n%10;
a[i]=temp;
n=(n-temp)/10;
i++;
}
while (n!=0);
int sum = 0;
for(int j=0; j<=5; j++)
{
exponent_of_10(a[j],j);
S = exponent_of_10(a[j],j);
if (S==0)
{break;}
sum +=S;
}
if(sum==n)
{
cout<< "Congratz, this is PALIDROMIC NUMBER !!";
}
else
cout<< "Sad life, this is NOT palidromic number";
return 0;
}
When the code exits the do ... while() loop, n is 0. For the test in the if to be meaningful, the code should save the original value of n somewhere and compare sum to that original value.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am new to C++. I want to make a array of string in which I want to save names of players of a cricket team. I want to save this in the form of array like below:
player[1]= sachin;
player[2]= john smith;
I used program below but I am getting following error:
Error 1
error C2661: 'std::basic_istream>::getline' : no overloaded function takes 1 arguments
#include "stdafx.h"
#include <iostream>
#include<string>
int main()
{
using namespace std;
string player[3];
int i;
for (int i = 1; i <= 3; i++)
{
std::getline(cin, player);
}
}
How can I save string array like we do with numerical array.
Replace for (int i = 1; i <= 3; i++) with for (int i = 0; i <= 2;
i++)
getline() often causes errors and it wont simply work if you are using some flavor of Linux.
You are not indexing the array properly as in it should have been player[i] instead of just player.
if you are using using namespace std; then a better way of declaring is to declare it outside the main function.
Following code should do what you are trying to do.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
string player[3];
int i;
for (int i = 0; i < 3; i++) {
cin >> player [i];
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to create a program that searches how many comments (// symbols) are in .txt file.
This is my code:
#include <fstream>
using namespace std;
const char read[] = "read.txt";
const char result[] = "result.txt";
const int CMax = 256;
void Skaityti (char E[], int& n);
int main() {
char E[CMax];
int n,k;
Skaityti(E,n);
ofstream rs(result);
rs << k;
return 0;
}
void Skaityti (char E[], int & n)
{
ifstream fd(read);
int k;
char sim = '/';
for (n = 0; !fd.eof() && n < n+1; n++)
fd.get(E[n]);
for(int i = 0; i < n;i++) {
if(sim == E[n])
k++; }
fd.close();
}
Program read fine, but I can't get symbol from massive.
I'm confused by your question... but I think you're asking to see how many times "//" appears in a file. I threw this together:
#include <fstream>
using namespace std;
int Skaityti()
{
ifstream fd("test.txt");
int count = 0;
while(fd.good())
{
char c = fd.get();
if(fd.good())
if(c == '/')
{
c = fd.get();
if(fd.good())
if(c == '/')
count++; // At this point we have two comments in a row
}
}
fd.close();
return count;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
please someone help me finding the error in this code. i've its python counterpart which is working fine, but when i code this in c++ , giving values(N) >=5 gives me an infinite loop.
[in this problem, you have to find the no. of trailing zeros in the factorial of a given number]
[here T is no. of test cases, N is the number, and z is output]
#include <iostream>
using namespace std;
int main(){
long int T,N,Z;
cin>>T;
for(int x=0;x<T;x++){
Z=0;
cin>>N;
for(int y=1;y<=N;y++){
if(y%5==0){
while(y/5!=0 && y%5==0){
Z+=1;
y/=5;
}
}
}
cout<<Z<<endl;
}
}
my python code is (which is working fine)
T=int(raw_input())
for x in range(1,T+1):
Z=0
N=int(raw_input())
for y in range(1,N+1):
if y%5==0:
while y/5!=0 and y%5==0:
Z=Z+1
y=y/5
print Z
change this:
for(int y=1;y<=N;y++){
int temp=y;
if(temp%5==0){
while(temp/5!=0 && temp%5==0){
Z+=1;
temp/=5;
}
}
}
your logic is not correct. Look at this, which i correctly submitted , hope it helps:-
#include<iostream>
using namespace std;
int tailing_zero(int n){
int ans=0;
int DIV=5;
while(n/DIV >0){
ans=ans+n/DIV;
DIV=DIV*5;
}
return ans;
}
int main(){
int run_count;
cin>>run_count;
int input[run_count];
for(int i=0;i<run_count;i++){
cin>>input[i];
}
for(int i=0;i<run_count;i++){
cout<<tailing_zero(input[i])<<endl;
}
return 0;
}