Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This script is supposed to read chars from keyboard, store them into arrays, and then output them:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void storeArraysintoStruct(char[], int);
int main()
{
char test[] ="";
int a = 0;
storeArraysintoStruct(test, a);
system("pause");
return 0;
}
void storeArraysintoStruct(char test[], int a)
{
int n;
cout << "Enter number of entries: " << endl;
cin >> n;
int i = 0;
for (i=0, i<n, i++)
{
cout << "Enter your character: " << endl;
cin.getline(test, n);
}
while (i < n)
{
cout << test[i] << endl;
i++;
}
}
Edit: fixed it:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void storeArraysintoStruct(char[], int);
int main()
{
char test[40] = "";
int a = 0;
storeArraysintoStruct(test, a);
system("pause");
return 0;
}
void storeArraysintoStruct(char test[], int a)
{
int n;
cout << "Enter number of entries: " << endl;
cin >> n;
int i;
for (i=0; i < n; i++)
{
cout << "Enter your character: " << endl;
cin >> test[i];
if (test[n-1])
{
cout << endl;
}
}
i =0;
while (i < n)
{
cout << test[i] << endl;
i++;
if(test[n-1])
{
cout << endl;
}
}
}
However, I am getting the errors Expected: primary expression before ")" and ";" before while. Any help will be greatly appreciated.
Edit: The script doesn't work as expected, for it doesn't output the stored characters. Any advice would be greatly appreciated.
The syntax error has already been pointed out in the comments. Also, as it has been mentioned, you never reset i after for loop, which prevents your while loop from running.
However, you have to also take in mind that this
char test[] = "";
allocates array test of only 1 character long. You cannot put more than one character of data into that array. In other words, your storeArraysintoStruct is sure to overrun the array and fall into undefined behavior territory.
In you want to preallocate a larger buffer for future use in storeArraysintoStruct, you have to specify the size explicitly. For example
char test[1000] = "";
will make test an array of 1000 characters. Of course, regardless of how large the array is, it is your responsibility to observe the size limit.
P.S. What is the point of that parameter a, if you never use it inside storeArraysintoStruct?
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to write a function that reads a 2D array of integers where the user is the one who enters the size of the array (it shouldn't be defined before ) . I tried this but it's not working.
I appreciate your help!
line 6 is the declaration of the function
this is my code:
#include <iostream>
using namespace std;
void Fill_table(int mat[][], int s) {
for (int i = 0; i<s; i++) {
for (int j = 0; j<s; j++) {
cout << "mat[" << i << "][" << j << "]:" << endl;
cin >> mat[i][j];
}
}
}
int main()
{
int n;
cout << "Enter the size: ";
cin >> n;
int a[n][n];
Fill_table(a, n);
return 0;
}
Just to give you a simple alternative. Instead of declaring a 2D array with [][], you could use [n*n] and then subscript with i*s + j. The code would be
#include <iostream>
using namespace std;
void Fill_table(int* mat, int s) {
for (int i = 0; i<s; i++) {
for (int j = 0; j<s; j++) {
cout << "mat[" << i << "][" << j << "]:" << endl;
cin >> mat[i*s+j];
}
}
}
int main()
{
int n;
cout << "Enter the size: ";
cin >> n;
int a[n * n];
Fill_table(a, n);
return 0;
}
1D arrays can be passed as a pointer to a function. You also need to pass the size (as you did). For 2D arrays, you need to know the size at compile time.
You cannot set the size of a multidimensional array at runtime.
The error
error: declaration of ‘mat’ as multidimensional array must have bounds for all dimensions except the first
void Fill_table(int mat[][], int s)
tells you exactly that.
Consider using C++ data structure instead, in this case vector<vector<int>>, you have a somewhat simple how-to guide in Creating a Matrix using 2D vector in C++ – Vector of Vectors.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
The new typist in the printing cell is typing carelessly the jobs assigned. The typist while was supposed to type all the characters in upper case, has got in lower cases too. Your duty is to verify if all the characters are in upper case and do so if not. Also, notify how many mistakes the typist did.
Input bEGIN
Output BEGIN 1
I am getting wrong answer in some of the cases please help i am beginner
n=length of string
1<=n<=50
int main() {
string s;
cin >> s;
int ans = 0;
for (auto &c : s) {
if (islower(c)) {
ans++;
c = toupper(c);
}
}
cout << s;
cout << endl;
cout << ans;
return 0;
}
I think it includes spaces too, so instead of using >> operator use getline(cin,string), as >> gets terminate when whitespace is occurred.
#include <iostream>
using namespace std;
int main() {
string s;
getline(cin,s);
int ans = 0;
for (auto &c : s) {
if (islower(c)) {
ans++;
c = toupper(c);
}
}
cout << s;
cout << endl;
cout << ans;
return 0;
}
This might be a solution to other test cases.
This should work:
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
int main() {
string s;
getline(cin,s);
int ans = 0;
for (auto &c : s) {
if (islower(c)) {
ans++;
c = toupper(c);
}
}
cout << s;
cout << endl;
cout << ans;
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to make a login page. When the program starts, the user has to type Guest and Password 1234 and he can edit his/her account. However, when I try to run it, it says:
Line 15 "Error incompatible types in assignment of 'const char[6]' to 'char[20]'
Line 16 "Error incompatible types in assignment of 'const char[5]' to 'char[20]'
I think it has to do with pointers but I am still a c++ newbie so I am having a hard time to understand pointers
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
const int LINE_LENGTH=20;
const int ID_LENGTH=8;
struct profile{char user[20];char password[20];double CGPA; int ID;};
int main(){
int count=0, i;
profile student[10];
student[0].user="Guest"; //Line 15
student[0].password="1234"; //Line 15
char signupName[20];
char signupPassword[20];
while (count==0)
{
cout << "#############################################\n";
cout << " Welcome to my program! \n";
cout << " Sign up to get started \n\n\n";
cout << " If you are starting, use username 'Guest' \n";
cout << " and password '1234' \n\n";
cout << "Username: ";
cin >> signupName;
cout << "Password: ";
cin >> signupPassword;
cout << "#############################################\n";
for (i=0;i<11; i++)
{
if(strcmp(signupName,student[i].user)==0 && strcmp(student[i].password,signupPassword)==0)
{
count++;
}
}
if(count==0)
{
system("cls");
cout<<"Your username and/or password is incorrect\n";
}
}
system("cls");
}
You need two minor changes to your code! First, as Francois Andrieux says, you can't assign char array strings with = ...
// student[0].user = "Guest";
// student[0].password = "1234";
strcpy(student[0].user, "Guest");
strcpy(student[0].password, "1234");
Second, your for loop runs once to often:
// for (i = 0; i < 11; i++)
for (i = 0; i < 10; i++) // Note: The last element in an array of 10 is x[9]!
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 3 years ago.
Improve this question
I have to write a program in which integer value is entered from user and the string has to be displayed that many times. But I am getting errors.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int N;
cout << "Enter N: ";
cin >> N;
cout << string(N, "Well Done");
return 0;
}
Note: I am not permitted to use a loop in this assignment.
If you may not use a loop, you may use goto to get around the restriction:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int N;
cout << "Enter N: ";
cin >> N;
{
int i = 0;
goto test;
begin:
cout << "Well Done";
++i;
test:
if (i < N)
goto begin;
}
return 0;
}
Note that goto is widely considered bad practice.
EDIT2: IN THE ORIGINAL ASKER's COMMENTS, LOOPS OF ANY KIND ARE PROHIBITED IN THIS ASSIGNMENT.
Use recursion.
void printN(int n, string s) {
if (n <= 0) return;
cout << s << endl;
printN(n-1, s);
}
Then you can call this from your main program as follows:
printN(userInput, "Hi my name is ricky bobby");
EDIT: just saw you haven't learned recursion yet. Look up this term, and familiarize yourself with it. This is a way to do iteration without looping (this is the most simplistic way I can describe it)
std::string does not have a constructor that repeats a string N times (it does have one for repeating a single character N times, though). What you need is a loop instead, eg:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int N;
cout << "Enter N: ";
cin >> N;
for (int i = 0; i < N; ++i)
cout << "Well Done";
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
#include <iostream>
#include <string>
using namespace std;
bool custNum(char [], int);
int main()
{
const int size = 8;
char custmor[size];
cout << "Enter a customer number in the form ";
cout << "LLLNNNN\n";
cout << "(LLL = letters and NNNN = numbers): ";
cin.getline(custmor, size);
if(custNum(custmor, size))
cout<<"That's a valid id number"<<endl;
else
cout<<"That's not a valid id number"<<endl;
return 0;
}
bool custNum(char custNum[], int size)
{
int count;
for(count = 0; count<3; count++)
{
if(!isalpha(custNum[count]))
return false;
}
for(count = 3; count <size - 1; count++) //3<7 , 4
{
if(!isdigit(custNum[count]))
return false;
}
return true;
}
so I want to loop through a character array of 3 letters and 4 numbers like ABC1234, but I didn't get the condition of the second for loop (size - 1). How does it work every time it tests the condition?
Never use count as a loop variable. A good name for a loop variable is i.
Never declare variables away from their initialization. The above should be for( int i = 0; ... in both cases.
i < size - 1 is probably wrong. What you probably want is i < size.
Anyhow, it would help if you showed how size is declared, how it is initialized, etc. It would also help if you showed the exact text you are trying to parse. It would also help if you explained exactly what you expected to happen, and exactly what happened instead. I might amend my answer when you do that.
you read only amount of characters that size variable specify,
since then , Why custNum function would not return true for anything longer than size variable ? , Because it's not checking anything more than what size variable specify.
Below is the code you need
#include <iostream>
#include <string>
using namespace std;
bool custNum(string,unsigned int);
int main()
{
const unsigned int size = 8;
//char custmor[size];
string mystring;
cout << "Enter a customer number in the form ";
cout << "LLLNNNN\n";
cout << "(LLL = letters and NNNN = numbers): ";
cin >> mystring;
cout << mystring <<endl << " " << mystring.length() << endl;
// cin.getline(custmor, size);
if(custNum(mystring , size))
cout<<"That's a valid id number"<<endl;
else
cout<<"That's not a valid id number"<<endl;
return 0;
}
bool custNum(string s, unsigned int size)
{
unsigned int count;
if (s.length() != (size + 1))
return false;
for(count = 0; count<3; count++)
{
if(!isalpha(s[count]))
return false;
}
for(count = 3; count <size - 1; count++) //3<7 , 4
{
cout << s[count] <<endl;
if(!isdigit(s[count]))
return false;
}
return true;
}